Results 1 to 4 of 4
  1. #1
    blug is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default Bit confused with inheritance

    Hey, I have been recently doing an assignment and have gotten myself a bit confused.

    Essentially, I have 4 different classes, 1 of which is a base class and 3 of which are derived from the base class.

    I now have an array which is of the same type as the base class, and if I want to insert a derived class into that array, I can do so because it inherits it's parent classes type.

    Now this is where I am confused, how do I access derived classes methods when trying to access the classes through the array? It only allows me to access the base types methods.

    THANKS!
    Last edited by blug; 11-22-2010 at 12:36 PM.

  2. #2
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    it is not possible for u to access the method which is in derive class but not in base class as per inheritance concept bcoz u only have reference of type base class so it know about mehtod swhich are in base class

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    You post the code template and we will help you from the place where you got stuck up.
    Last edited by RamyaSivakanth; 11-22-2010 at 01:29 PM.
    Ramya:cool:

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by blug View Post
    Essentially, I have 4 different classes, 1 of which is a base class and 3 of which are derived from the base class.

    I now have an array which is of the same type as the base class, and if I want to insert a derived class into that array, I can do so because it inherits it's parent classes type.

    Now this is where I am confused, how do I access derived classes methods when trying to access the classes through the array? It only allows me to access the base types methods.
    You know that an object in the array is a derived class object but nothing else knows about that. You either have to cast the object you get from the array to the derived class explicitly or you have to make all methods available to the base class or you have to write a visitor object.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Confused :?
    By jESTPHROSTY in forum New To Java
    Replies: 2
    Last Post: 10-17-2010, 07:38 PM
  2. I am confused
    By prof.deedee in forum New To Java
    Replies: 6
    Last Post: 10-30-2009, 11:32 PM
  3. Confused
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 01-13-2009, 01:00 PM
  4. a lot confused
    By vineethraj in forum New To Java
    Replies: 4
    Last Post: 01-18-2008, 12:36 AM
  5. what does it mean:confused:
    By sivasayanth in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 04:52 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
  •