This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
 
Don't get me started about those stupid light bulbs.
 
subject: Instance field member and inheritance
 
Similar Threads
Generics, Collection Help: Sorty Object based on Name
Inner Class
JMK MAKE
TIJ 3 problem at simpeltest
static method not running