• 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

what is output of below code and why??

 
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 41
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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() )
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic