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
Joined: Mar 03, 2007
I Appreciate your valuable time.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com