• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Inheritance

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Can somebody help me understand why this code:




giving the following results :
Child
Parent
I understand why the "Child" is appearing - the overridden method "someMethod()" will be invoked and it will use the "s" variable from the Child class. That is why I don't know why the show() method will use the "s" variable from the Parent class in the second case (line 9). The Child object is created and the show() method is inherited by the Child class.

Any help will be appreciated
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kamila Jolan wrote:That is why I don't know why the show() method will use the "s" variable from the Parent class in the second case (line 9). The Child object is created and the show() method is inherited by the Child class.



Inheriting doesn't mean that the compiler will get the source for the parent method and recompile it for the child. If the child doesn't override the method, the parent's version of the method is called -- and the parent's version of the show() method only knows about the parent reference the string. Polymorphism doesn't apply to instance variables.

Henry
 
Ranch Hand
Posts: 47
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Kamila Jolan wrote:That is why I don't know why the show() method will use the "s" variable from the Parent class in the second case (line 9). The Child object is created and the show() method is inherited by the Child class.



Polymorphism doesn't apply to instance variables.

Henry



The statement quoted by Henry is the main concept. As Polymorphism applies only for the methods (only instance not even static methods), JVM took the polymorphic method show() but for the field it took original field of the class.

Thanks.
 
Kamila Jolan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic