• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance field member and inheritance

 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic