Java - Polymorphism
Java - Polymorphism
Polymorphism in Java
Polymorphism is the ability of an object to take on many forms. Polymorphism is an important feature
of Java OOPs concept and it allows us to perform multiple operations by using the single name of any
method (interface). Any Java object that can pass more than one IS-A test is considered to be
polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for its
own type and for the class Object.
It is important to know that the only possible way to access an object is through a reference variable. A
reference variable can be of only one type. Once declared, the type of a reference variable cannot be
changed.
The reference variable can be reassigned to other objects provided that it is not declared final. The type
of the reference variable would determine the methods that it can invoke on the object.
A reference variable can refer to any object of its declared type or any subtype of its declared type. A
reference variable can be declared as a class or interface type.
Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a
certified expert to boost your career.
Now, the Deer class is considered to be polymorphic since this has multiple inheritance. Following are
true for the above examples −
A Deer IS-A Animal
When we apply the reference variable facts to a Deer object reference, the following declarations are
legal −
All the reference variables d, a, v, o refer to the same Deer object in the heap.
Open Compiler
interface Vegetarian{}
class Animal{}
public class Deer extends Animal implements Vegetarian{
public static void main(String[] args) {
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
Output
true
true
true
true
This example has multiple methods having the same name to achieve the concept of compile-time
polymorphism in Java.
Open Compiler
Output
Run time polymorphism is also known as dynamic method dispatch and it is implemented by the
method overriding.
Open Compiler
Output
I have a Car.
I have a Bike.
We already have discussed method overriding, where a child class can override a method in its parent.
An overridden method is essentially hidden in the parent class, and is not invoked unless the child class
uses the super keyword within the overriding method.
Now, you study the following program carefully and try to determine its output −
Open Compiler
class Employee {
private String name;
private String address;
private int number;
Output
Constructing an Employee
Constructing an Employee
While invoking s.mailCheck(), the compiler sees mailCheck() in the Salary class at compile time, and the
JVM invokes mailCheck() in the Salary class at run time.
mailCheck() on e is quite different because e is an Employee reference. When the compiler sees
e.mailCheck(), the compiler sees the mailCheck() method in the Employee class.
Here, at compile time, the compiler used mailCheck() in Employee to validate this statement. At run
time, however, the JVM invokes mailCheck() in the Salary class.
This behavior is referred to as virtual method invocation, and these methods are referred to as virtual
methods. An overridden method is invoked at run time, no matter what data type the reference is that
was used in the source code at compile time.