aspose file tools*
The moose likes Beginning Java and the fly likes what happens at runtime? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what happens at runtime? " Watch "what happens at runtime? " New topic
Author

what happens at runtime?

aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111

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
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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

Joined: Mar 03, 2007
Posts: 111


I Appreciate your valuable time.

Thank you

regards
samura
 
Consider Paul's rocket mass heater.
 
subject: what happens at runtime?