• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism ...how to deal with variables  RSS feed

 
sudip Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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....
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
You're welcome! And welcome to the Ranch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!