Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Inheritance question

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Inheritance question

    Class Animal{

    int i = 10;
    public void eat()
    {
    S.O.P("ANIMAL")
    }
    }

    Class Cat extends Animal
    {
    int i = 20;
    public void eat()
    {
    S.O.P("CAT")
    }
    }

    If i instantiate

    Animal aa = new Cat();
    s.o.p(aa.i);
    s.o.p(aa.eat());

    aa.i will print 10 and aa.eat() will print CAT. WHy is it so? aa.i should print 20 value only rite?
    Can someone explain me the reason for this?


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Inheritance question

    The Object you are making is an Animal Object. By having Cat extend Animal you can store a Cat inside an Animal object if you choose.
    When you made a new Cat object it should over ride all same named methods, it must not override class variables the same way.
    Ill dig up an old Java book and see if it explains why.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Inheritance question

    (you should probably use examples that compile by the way )

    Okay, so specifically you are using the concept called Polymorphism. "A superclass reference variable can reference objects of a subclass"

    so because of dynamic binding, your animal object will check to see what kind of object is actually has during run time,
    then when it calls its method "s.o.p" the override method will take over because of polymorphism. This you saw working when your method printed 20.

    I found this googling
    "overiding class variables java"

    "In short, no, there is no way to override a class variable.

    You do not override class variables in Java you hide them. Overriding is for instance methods. Hiding is different from overriding.

    In the example you've given, by declaring the class variable with the name 'me' in class Son you hide the class variable it would have inherited from its superclass Dad with the same name 'me'. Hiding a variable in this way does not affect the value of the class variable 'me' in the superclass Dad."

    so since the Animal Object is just POINTING to the address of a CAT Object, I think it uses the ANIMAL "i" instance variable.

    I could be a little off, anyone want to double check my wording?

Similar Threads

  1. Inheritance
    By lewzax in forum Object Oriented Programming
    Replies: 4
    Last Post: July 8th, 2011, 01:51 PM
  2. Java Inheritance Help
    By danielparry in forum Java Theory & Questions
    Replies: 3
    Last Post: March 17th, 2011, 03:20 PM
  3. Inheritance and Overriding help!
    By Knserbrave in forum Object Oriented Programming
    Replies: 4
    Last Post: February 24th, 2011, 01:46 PM
  4. inheritance help
    By justin3492 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 07:45 PM
  5. inheritance
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2010, 09:23 PM