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.
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 Dog implements Animal {
public void move() {
System.out.print("Running...");
}
}
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
animal = new Dog();
|
Because Bird is
an Animal, it’s legal to write:
Animal animal = new Bird();
|
Likewise, it’s
perfect to write:
Animal animal = new Fish();
|
As you can see, we
declare a reference variable called animal, 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 Trainer class as
follows:
public class Trainer {
public void teach(Animal animal) {
animal.move();
}
}
|
Notice that the teach() method
accepts any kind of Animal. Thus we can pass any objects which are sub types of the Animal type.
For example:
Trainer
trainer = new Trainer();
Animal dog = new Dog();
Animal bird
= new Bird();
Animal 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 Animal interface.
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 Bird object)
Swimming… (from the Fish 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:
1.
Reusability: the
teach() method can be re-used for different kinds of objects as long as they
are sub types of the Animal interface.
2.
Flexibility: the
actual object can be determined at runtime which allows the code run more
flexibly.
3.
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.
[Core Java Interview Questions]
We recommend you take Big Data Hadoop class room training at eMexo Technologies in electronic city, Bangalore to learn more about Big Data Hadoop.
0 Comments:
Post a Comment