• 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 happens at runtime?

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

output:


inside importantMethod() of Tea class
inside importantMethod() of Tea class
inside importantMethod() of Beverage class
Inside tea class

I expected line 2 prints inside importantMethod() of Beverage class.
the reference variable(Beverage bobj = tobj) bobj is of type Beverage and
the object is tea.

may i know what's happening during run time?

Thanks in Advance

 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the reference variable(Beverage bobj = tobj) bobj is of type Beverage and the object is tea



Correct. And an object acts as the object, not as its reference. So when you call any methods on an object, it uses the object's overridden methods, if present, not the reference's methods. If we added a method onlyInBeverage(), and only put it in Beverage, and did not override it in Tea, the method from Beverage will get called regardless of whether the reference is of Beverage (bobj), or Tea (tobj).

The other part of this, however, is you can only call methods that are present in the reference type. So if your Tea class had a method named brew() (that was only in Tea, but not in Beverage), you could call tobj.brew() (since the reference is of type Tea), but you could not call bobj.brew() - even though the object is of type tea, the reference is of type Beverage. Therefore the reference bobj does not know anything about a method named brew(). If you tried to do such, your code would not even compile.

This is the power of polymorphism. At coding time and compile time you may only know that something is going to be a Beverage, but are not sure what type of Beverage. So you make the reference a Beverage reference. Then at Runtime, any class that extends Beverage, whether it was Tea, Coffee, Soda, or Juice, can be used. You know you can call any method defined in Beverage, such as flavor(), but get the actual class's overridden implementation, such as "sweet" for juice and "carbonated" for soda, etc.

Take a look at the Campfire Story How my Dog learned Polymorphism here at JavaRanch. It has a silly name, but does a great job at explaining polymorphism, as does the book Head First Java, 2nd Edition. In my opinion, Head First Java has the best explanation of Polymorphism I have ever seen. These will help you better understand Polymorphism, which is one of the most powerful things in Object-Oriented Programming (OOP). And cool too
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I Appreciate your valuable time.

Thank you

regards
samura

reply
    Bookmark Topic Watch Topic
  • New Topic