Results 1 to 4 of 4
Thread: Getting around inheritence
- 01-18-2009, 06:22 AM #1
Member
- Join Date
- May 2008
- Posts
- 27
- Rep Power
- 0
Getting around inheritence
I am still on the topic of inheritance, but this issue maybe due to the requirements of the work, just wanted to see if there is a way around it.
So i have an addItem method in my Borrower class
This method is overridden in its subclassJava Code:public void addItem(LibraryItem libraryItem) { itemsBorrowed[i] = libraryItem; i++; }
Now in a tottally seperate class called library, which basically acts as my controller, i want to add an item to a borrowers personal array.Java Code:/**Overridden addItem method from Borrower class*/ public void addItem(LibraryItem libraryItem) { if(staffItems>MAX_ITEMS) //If staff has more items that are allowed { System.out.println("The maximum number of items allowed is 10" + "\n" + "You currently have " + MAX_ITEMS + "out at the moment"); } else { super.addItem(libraryItem); staffItems++; } }
Now the Library class has its own arrays to store users and items in the library
Now when i come to loan an item to a user, using the addItem method, i realistically want it too call the overridden subclass method as seen above. But to do this, in my library class i am using the following codeJava Code:private Borrower[] libraryUsers = new Borrower[50]; //Libraries Borrower array private LibraryItem [] libraryItems = new LibraryItem[50]; //Libraries LibraryItem array
Which i was hoping would point to a specific user in the library and use the addItem method to add an item to their personal array. Well, it does, but i get an arrayIndexOut ofBounds exception if i try to add more than 10 items whic tells me is that it is doing it through the addItem method in the superclass (As the warning i created in the subclass isnt being thrown). This then in my opinion is because libraryUsers[user]. is instantiating my Borrower class, and not the subclass, hence its the method in the Borrower class which is called. If i am right, is there any logical way of resolving this issue, without changing the array type?Java Code:libraryUsers[user].addItem(libraryItems[item]);
cheers
-
I think I've seen some of your code before in the Sun forums. If I remember correctly, I believe that your Borrow class is abstract, and if so it is impossible for it to be instantiated.
What you should do is step through your code with a debugger to see exactly what is going on just before and during the throwing of the exception. If this fails, then create an SSCCE and post it here.
Good luck.
EDIT:
I've just done a quick search of your posts on the Sun site and have come up with this thread begun by you that confirms that your Borrower class is in fact abstract:
Java Programming - question about inheritence
The thread also has some decent discussion about your inheritance issues and their solutions. You would do well to post this and similar links in future posts of this nature.Last edited by Fubarable; 01-18-2009 at 06:42 AM.
- 01-18-2009, 06:45 AM #3
Member
- Join Date
- May 2008
- Posts
- 27
- Rep Power
- 0
yeah, this topic has been on sun forums, good help there, but its been dead all night, only 2 members logged in. The Borrower class is abstract, but that doesnt stop you from creating an array of that type. And then if you call a method through this array, like i have done, i suppose that is instantiating. Everything compiles fine, maybe its doing somthing else then. I will go back step by step and try to debug it.
thanks
-
You are absolutely correct here. You can declare and instantiate an array of an abstract class or even an interface.The Borrower class is abstract, but that doesnt stop you from creating an array of that type.
But here you are wrong. You cannot instantiate an abstract class or interface. Period.And then if you call a method through this array, like i have done, i suppose that is instantiating.
I think that you're on the right track here.Everything compiles fine, maybe its doing somthing else then. I will go back step by step and try to debug it.
Good luck and good night (gracious! it's almost 1 am here!)
Similar Threads
-
How to get objects to interact without inheritence in separate classes?
By Fliz in forum New To JavaReplies: 1Last Post: 11-18-2008, 04:48 PM -
[SOLVED] Problem with code - inheritence
By yalla in forum New To JavaReplies: 1Last Post: 03-30-2008, 06:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks