posted 23 years ago
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