Forums Register Login

**Inheritance and member variable question**

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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);

}
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 832 times.
Similar Threads
Hiding of instance variables. Hard question?
Shadowing attribute of superclass in subclass
Member variable overriding problem
Can sumone explain this to me? regarding attributes
Overriding and shadowing
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:53:27.