File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes **Inheritance and member variable question** Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "**Inheritance and member variable question**" Watch "**Inheritance and member variable question**" New topic
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, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: **Inheritance and member variable question**