• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

variable inheritance

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Given the following code:

If subclass's variable declaration is commented out, a different result will be printed out. Please let me know how variable inheritence works here. Thanks.
Luk
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object instantiated is a sub class of Person and in sub class the variable and methods of super class are hidded. So, if you hide the sub class variable it automaticall takes super class variable because it is protected variable in this case, you don't need to instantiate the class and call the variable like
Person p = new Person();
p.age;
you don't need this approach here.
Please correct me if I am wrong.
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but,
I try, they all come out as Person age = 21
Whatever I comment out age = 18 or not.
 
nan sh
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but,
I try, they all come out as Person age = 21
Whatever I comment out age = 18 or not.
 
V Srinivasan
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry luk,
I have overlooked it.
When created object of s1 both the classes constructor is triggered and the method is in super class and it knows only super class variable, it does't know sub class variable and that is why you are getting output as 21. When you call this method it first checks in Student class if not available it checks its super class Person and if it finds there it fires that method.
This is what I understand now. Hiding is not taking place in this scene. If you want to call the sub classes variable first modify the method which takes a parameter and in main method instantiate as I posted above and pass that as parameter in the method like
s1.print(s.age);
I hope this will work.
Thanks,
 
luk Hann
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
v,
Thanks for your message.
The print method in main is invoked, the control will go to Studnet constructor, and since in Student class, there is no print method, it goes one way up to parent class which is Person, and find the constructor Person and take the value 21, then print it out. Until now, I am fine. (BTW, if there is another parent class for Person, named Human with a constructor Human(), will it go further way up to Human?)
My questions is: if I comment out the "protected int age" in subclass Student,(nan, this is your problem) why it will take the value 18. I guess it relates to variable inheritence. Could you explain that for me? Does that mean it didn't go to parent class's constructor at this moment? I hardly understand this.
Also, I tried your way, it works and prints out age = 18. Could you explain why it works? (in this case, the variable declaration in Student class doesn't have any influence to the result.) Thanks.
Luk
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Folks,
When the code above is run, here's the sequence of events when the new Student is constructed:
1) Student constructor calls its parent constructor, Person().
2) Person constructor sets Person.age to 21 and returns.
3) Student constructor sets Student.age to 18 and returns.
Now, Person.age is 21 and Student.age is 18.
When s1.print() is called, this resolves to the print() method in Person, which prints the age variable it can see, which is Person.age which is 21.
However, if you comment out Student's age variable, here is what happens instead:
1) Student constructor calls its parent constructor, Person().
2) Person constructor sets Person.age to 21 and returns.
3) Student constructor sets Person.age to 18 (there is no Student.age, so the "age" reference here resolves to Parent.age) and returns.
Now, of course, calling s1.print() prints 18, since that's what it's value is.

[This message has been edited by Bill Compton (edited February 15, 2001).]
 
luk Hann
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill,
Thanks for your explaination. Very clear. But please allow me to ask a silly question: what does that Parent.age refer to? Thanks.
Luk
 
Bill Compton
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uhg! Sorry! That should have said "Person.age". I fixed it above.
 
luk Hann
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill,
It was thoroughly clear. Thanks. The last question: how about "grandpa" class?
Luk
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic