aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Instance field member and inheritance 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 "Instance field member and inheritance " Watch "Instance field member and inheritance " New topic
Author

Instance field member and inheritance

Unmesh Chowdhury
Ranch Hand

Joined: Jun 20, 2010
Posts: 45
Consider the following examples:

Example 1:

Example 2:

Example 3:

All of the instance field member resolution is done by the JVM but according to the type of the reference variable, that is, the instance field member resolution is not done according to the actual type of the object on the heap which is referred by the reference variable. Thus, the instance field member is also not eligible for runtime polymorphism.

I imagine that there are multiple parts of a polymorphic object in the heap, and the added parts are grown there due to the superclass instances.


M.Sc. in CS, OCPJP6 93%
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Unmesh Chowdhury wrote: Thus, the instance field member is also not eligible for runtime polymorphism.

Correct . that is why it is *better practice to provide setter/getter to an class instead exposing public instance variable directly to other class*. even you can hide the super class instance variable in subclass using private modifier , hence compile time error if you try to access the super class variable using subclass instance --> which violate liskov substitution principle.

hth
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance field member and inheritance