You are on page 1of 3

Polymorphism

Dynamic Binding

Consider an abstract class MusicalInstrument, with the abstract method


playMusic().

public abstract class MusicalInstrument


{
public abstract void holdInstrument();

public void playMusic()


{
holdInstrument();
//code to play music by the currently held
instrument.
.
.
.
}
}

Then we derive Guitar, Violin and Piano classes. Each with its own
implementation of holdIntrument().

public class Guitar extends MusicalInstrument


{
//instance variables
//Constructor Definition
.
.
public void holdInstrument()
{
//Code to initialize the guitar as the current
instrument.
}
}

public class Violin extends MusicalInstrument


{
//instance variables
//Constructor Definition
.
.
public void holdInstrument()
{
//Code to initialize the violin as the current
instrument.
}
}
public class Piano extends MusicalInstrument
{
//instance variables
//Constructor Definition
.
.
public void holdInstrument()
{
//Code to initialize the piano as the current
instrument.
}
}

We now define a driver program MusicApp to test our application,

public class MusicApp


{
public static void main(String[] args)
{
Guitar g = new Guitar();
g.playMusic();
}
}

The meaning of the method playMusic() depends on what instrument is held.


Because the definition of holdInstrument depends on the calling object. Java
will wait till runtime before it binds the method definition to its meaning. In our
example, the calling object is a Guitar, and the definition of the method
holdInstrument() in the class Guitar is the one that will be used. This is
possible because Java uses Dynamic Binding or Late Binding.

Indirectly Overridden Methods

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.

How much does Java Know?

Consider the following code,

MusicalInstrument m = new Guitar();


m.playMusic();

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,

Guitar g = new Guitar();


MusicalInstrument m = (MusicalInstrument)g;
m.playMusic();

Java won't be fooled.

Type Checking

When an Object is created with new, but is stored in a variable of an ancestor


class,

MythicalCreature a = new Dragon();

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.

Dynamic Binding with toString()

if you define a toString() method in your class, you can use it to display
information about the objects of your class.

Star s = new Star("Adhara", "Bright Giant");


System.out.println(s.toString());

You can even use the following line of code,

System.out.println(s);

The reason it works is that the overloaded method println has the following
method definition as one of its definitions.

public void println(Object someObject)


{
System.out.println(theObject.toString());
}

Thanks to Dynamic Binding Java uses the method definition of toString() in the
class Star,not in the class Object.

Polymorphism

Polymorphism comes from a Greek word meaning "many forms". Polymorphism in


computer science means the ability of the programming language to determine
which method definition will be used for a (directly or indirectly) overridden method.
Polymorphism is the concept itself, to achieve polymorphism Java uses Dynamic
Binding.

You might also like