Results 1 to 4 of 4
  1. #1
    nick2price is offline Member
    Join Date
    May 2008
    Posts
    27
    Rep Power
    0

    Default 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
    Java Code:
    public void addItem(LibraryItem libraryItem)
        {
            itemsBorrowed[i] = libraryItem;
            i++;
                
        }
    This method is overridden in its subclass
    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 in a tottally seperate class called library, which basically acts as my controller, i want to add an item to a borrowers personal array.
    Now the Library class has its own arrays to store users and items in the library
    Java Code:
     private Borrower[] libraryUsers = new Borrower[50];  //Libraries Borrower array
        private LibraryItem [] libraryItems = new LibraryItem[50]; //Libraries LibraryItem array
    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 code
    Java Code:
    libraryUsers[user].addItem(libraryItems[item]);
    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?
    cheers

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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 07:42 AM.

  3. #3
    nick2price is offline Member
    Join Date
    May 2008
    Posts
    27
    Rep Power
    0

    Default

    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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The Borrower class is abstract, but that doesnt stop you from creating an array of that type.
    You are absolutely correct here. You can declare and instantiate an array of an abstract class or even an interface.

    And then if you call a method through this array, like i have done, i suppose that is instantiating.
    But here you are wrong. You cannot instantiate an abstract class or interface. Period.

    Everything compiles fine, maybe its doing somthing else then. I will go back step by step and try to debug it.
    I think that you're on the right track here.

    Good luck and good night (gracious! it's almost 1 am here!)

Similar Threads

  1. Replies: 1
    Last Post: 11-18-2008, 05:48 PM
  2. [SOLVED] Problem with code - inheritence
    By yalla in forum New To Java
    Replies: 1
    Last Post: 03-30-2008, 07:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •