| Author |
**Inheritance and member variable question**
|
wonkty wang
Greenhorn
Joined: Jul 17, 2007
Posts: 29
|
|
Hi People, I got the following question from the mock exam... The answer is that "easy" will get printed. I just dont understand it why. As after inheritance, the subclass SCJPExam has it own copy of difficultyLevel, isn't?? Can anyone please explain here? Thanks
|
SCJP 5.0 <br />-> Change job<br />-> Decide another cert to do
|
 |
Srinivasan thoyyeti
Ranch Hand
Joined: Feb 15, 2007
Posts: 557
|
|
inheritance and polymorphism. Here you got confused with polymorphism. behavior(only instance-methods) can be over-ridden. FOR state(memebers either static or non-static) , THERE IS NO POLYMORPHISM concept like overloading or over-riding. in your example: Move following to Child class and test it; public void printDifficultyLevel(){ } System.out.println(difficultyLevel); }
|
Thanks & Regards,<br />T.Srinivasan,<br />SCWCD 1.4(89%),SCJP 5.0(75%)<br />"That service is the noblest which is rendered for its own sake." - Mahatma Gandhi
|
 |
wonkty wang
Greenhorn
Joined: Jul 17, 2007
Posts: 29
|
|
Thanks. Yeah, I know only the member method (not the member variable)could be overridden. However,the subclass does get a copy of member variable from the super class by inheritance. I guess my question would be: when get runs. why it access the superclass member variable "difficultyLevel" instead of the subclass ones. Thanks.
|
 |
Srinivasan thoyyeti
Ranch Hand
Joined: Feb 15, 2007
Posts: 557
|
|
Now in you are in real soup. A method in parent class(***not-over-ridden-in-child), How you can expect to access the state of child ? *** Remember if you instantiate Child --> parent also gets instantiated.
|
 |
wonkty wang
Greenhorn
Joined: Jul 17, 2007
Posts: 29
|
|
|
Thanks for your help. I think I got the whole basic concept wrong. Inheritance is the logic concept which means there is not a physically a copy of whatever member variable or member methods. I guess the subclass just has the access to the superclass's methods or variables.
|
 |
 |
|
|
subject: **Inheritance and member variable question**
|
|
|