• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Polymorphism ...how to deal with variables

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I am missing something basic over here...please help...
Please look at the below code:



The output in this case is
in child class
10

The parent reference invokes the method in the child class because of dynamic polymorphism....but should'nt it do the same thing for the variable also.
I mean obj.i actually calls the variable of the parent class and not of the object type ( child). But for methods it does call the method of the object type.

Please help.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Polymorphism only applies to non-static methods. Static methods and fields instead hide or shadow the method / field from the super class. The reference type (here it's Parent) decides which version to use, unlike the actual type for non-static methods.
 
sudip Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Rob,

Did you reply to this problem...the thread shows that there is 1 reply from you...but on cliking there is nothing...
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You still missing Rob's reply? Because I can see it OK.
 
sudip Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob

Was able to see your reply finally...

Static methods and fields instead hide or shadow the method / field from the super class. The reference type (here it's Parent) decides which version to use, unlike the actual type for non-static methods.



Do you mean that static methods and static variables only or even instance variables...

Here the case is with the instance variable which is being invoked in a static way as the parent reference calls the parent class'es variable....
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sudip Kumar wrote:
Do you mean that static methods and static variables only or even instance variables...



Runtime polymorphism in Java applies only to non-private, non-static, non-final methods. It does not apply to anything that is static, private, or final, and it does not apply to any member variables.

So, if we have



and then we refer to p.X, then if X is a member variable, or if it's private, static, or final, it is determined at compile time that we will get the X from whatever type reference "p" is declared to be--in this case, Parent. Only if X is a non-private, non-static, non-final method will we get X from the actual object that p refers to at runtime.

 
sudip Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thaks Jeff for that wonderful reply...really helps....
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome! And welcome to the Ranch!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic