1. What Is Polymorphism?

Polymorphism means ‘many forms’. In OOP, polymorphism means a type can point to different object at different time. In other words, the actual object to which a reference type refers, can be determined at runtime.

In Java, polymorphism is based on inheritance and overriding. So if you absorbed my lessons about inheritance and overriding the following tutorials:

You will understand polymorphism quickly.

 

2. How is Polymorphism Implemented in Java?

In Java, you can implement polymorphism if you have a super class (or a super interface) with two or more sub classes.

Let’s understand by looking at some examples.

Suppose that we have the following interface and classes:

public interface Animal {
	public void move();
}

public class Bird implements Animal {
	public void move() {
		System.out.print("Flying...");
	}
}

public class Fish implements Animal {
	public void move() {
		System.out.print("Swimming...");
	}
}
As you can see, we have Animal as the super interface, and 3 sub classes: Dog, Bird and Fish.

Because the Dog implements Animal, or Dog is an Animal, we can write:

Animal anim = new Dog();


Because Bird is an Animal, it’s legal to write:

Animal anim = new Bird();
Likewise, it’s perfect to write:

Animal anim = new Fish();
As you can see, we declare a reference variable called anim, which is of type Animal. Then we assign this reference variable to 3 different kinds of object: Dog, Bird and Fish.

You see? A reference type can take different objects (many forms). This is the simplest form of polymorphism, got it?

Now we come to a more interesting example to see the power of polymorphism.

Suppose that we have a trainer who teaches animals. We create the Trainerclass as follows:

public class Trainer {
	public void teach(Animal anim) {
		anim.move();
	}
}
Notice that the teach() method accepts any kind of Animal. Thus we can pass any objects which are sub types of the Animaltype. For example:

Trainer trainer = new Trainer();

Dog dog = new Dog();

Bird bird = new Bird();

Fish fish = new Fish();

trainer.teach(dog);
trainer.teach(bird);
trainer.teach(fish);
Outputs:

Running…
Flying…
Swimming…
Here, as you can see, the teach() method can accept ‘many forms’ of Animal: Dog, Bird, Fish,… as long as they are sub types of the Animalinterface.

In the teach() method, the move() method is invoked on the Animal reference. And depending on the actual object type, the appropriate overriding method is called. Thus we see the outputs:

Running… (from the Dog object).
Flying… (from the Dog object).
Running… (from the Dog object).
 

3. Why is Polymorphism?

Polymorphism is a robust feature of OOP. It increases the reusability, flexibility and extensibility of code. Take the above example for instance:

  • Reusability: the teach() method can be re-used for different kinds of objects as long as they are sub types of the Animal interface.
  • Flexibility: the actual object can be determined at runtime which allows the code run more flexibly.
  • Extensibility: when we want to add a new kind of Animal, e.g. Snake, we just pass an object of Snake into the teach() method without any modification.
That’s all about polymorphism I want to share with you today.

 

Related Tutorials:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#3HIMANSHU2020-03-08 23:20
Hi,

Thanks for your great tutorials. I have a doubt. Please help. What does the below statement exactly mean?

"the actual object can be determined at runtime" which allows the code run more flexibly.
Quote
#2Sushama2019-07-22 02:17
So method overloading is not form of polymorphism?
only inheritance and method overriding is polymorphism?
Quote
#1infoj2017-09-08 10:55
Polymorphism is an important OOPS concept and it gives the flexibility to design classes from more generic to more focused.
Quote