• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is output of below code and why??  RSS feed

 
yogesh doshi
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Dóra Takács
Ranch Hand
Posts: 39
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output -although you could have tried it yourself for practicing ;)- is the value of the variable 'i'.

Why? Because it is the whole point of polymorphism.
Although your reference's type is MySuper, the actual object being referenced is a MySub.
You have the method disp() in the MySuper superclass, this allows you to call that method, because the JVM knows there is such a method,
because the MySuper type reference obj tells it to it.

But since your object is of MySub type, calling the disp() method on it will invoke the MySub.disp() method.
This is because MySub only knows about its own method, because it overrid it.
(Except if inside the class, then you could invoke the superclass' method by super.disp() )
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, yogesh.

Just a note about your post. Please format your code and surround it with code tags. I'll do it for you this time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!