Professional Documents
Culture Documents
Dynamic Binding
Then we derive Guitar, Violin and Piano classes. Each with its own
implementation of holdIntrument().
The classes Guitar, Violin and Piano inherit the method playMusic() from
the abstract class MusicalInstrument. The method playMusic() itself is not
overriden in any of these classes. However, it contains a call to the abstract method
holdInstrument() that is overridden in each of these classes. Hence, we call
the method playMusic() an indirectly overridden method.
Java will be smart enough to figure out that the object MusicalInstrument was
created as a Guitar, and will play lovely guitar music.
Even if you use a type cast,
Type Checking
The variable (of type MythicalCreature) determines what methods will be used,
while the class used to create the object (the class Dragon) determines which
version of the method will be used.
if you define a toString() method in your class, you can use it to display
information about the objects of your class.
System.out.println(s);
The reason it works is that the overloaded method println has the following
method definition as one of its definitions.
Thanks to Dynamic Binding Java uses the method definition of toString() in the
class Star,not in the class Object.
Polymorphism