• Post Reply Bookmark Topic Watch Topic
  • New Topic

Isn't there runtime polymorphism for an instance variable ? Please help.  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even after studying java since last 1 year, still wondered today with the generic thing , "The runtime polymorphism ".
The dynamic method dispatch, well true for methods but is there any similar thing with the variables ?

Below program put more light ,





And the output is :


Height of rabbit with Animal reference:0
Height of rabbit with Animal reference using method:10
Height of rabbit with Rabbit reference :10

Can anybody explain this more ?
 
joni novhia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please correct me if my question is wrong or doesn't suite this discussion ..
Than ks
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shailesh pratapwar wrote:Even after studying java since last 1 year, still wondered today with the generic thing , "The runtime polymorphism ".
The dynamic method dispatch, well true for methods but is there any similar thing with the variables ?


Simply. No. All variable bindings are done at compile time -- which means it is determined by the reference type (and not by the object itself).


And I am not sure what you are trying to show with your example. Can you elaborate the confusion?

Henry
 
joni novhia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry for the reply.


In my program,
Animal an=new Rabbit();
Rabbit tom=new Rabbit();
I stored Rabbit into Animal 'an'.

My program depicts that, although I used Superclass reference to store subclass object then I will no longer in the state to access the "similar named variable in subclass" by using the that reference.

But this is not the case when it come to methods.
Hope you got this ....
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such thing as polymorphism when it comes to fields. The subclass fields are completely independent of the superclass fields.
You ought not to notice that problem because you ought to have marked all your fields private, so they are not accessible outside the class anyway. (Or only via getXXX methods.)
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shailesh pratapwar wrote:My program depicts that, although I used Superclass reference to store subclass object then I will no longer in the state to access the "similar named variable in subclass" by using the that reference.

Actually, you can, but it involves casting, which you should usually avoid, viz:
System.out.println("Height of rabbit with Animal reference cast to a Rabbit:" + ((Rabbit)an).h);

Winston
 
joni novhia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
shailesh pratapwar wrote:My program depicts that, although I used Superclass reference to store subclass object then I will no longer in the state to access the "similar named variable in subclass" by using the that reference.

Actually, you can, but it involves casting, which you should usually avoid, viz:
System.out.println("Height of rabbit with Animal reference cast to a Rabbit:" + ((Rabbit)an).h);

Winston


Yup .... agreed now ..... Thanks everyone for the replies ...
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better to use private variables and getMethodsThen you do not need to do anything to deal with height in the Rabbit class, or the Lion class. It is simply there, and the getHeight() method works for all subclasses. That is the whole idea of inheritance. You don’t even need to override it, which is why I marked it final.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!