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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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: 44
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Instance field member and inheritance
 
Similar Threads
JMK MAKE
static method not running
TIJ 3 problem at simpeltest
Generics, Collection Help: Sorty Object based on Name
Inner Class