We have already discussed “Compile time Polymorphism in java” in our previous post. If you have not gone through the topic I request you to please click here and read about What is “Polymorphism” and how “Compile time Polymorphism in java” actually works.
In this post, we
will be discussing “Runtime Polymorphism in java”.
What is Runtime Polymorphism?
Runtime polymorphism
or Dynamic Polymorphism is a mechanism in java by which the method building
takes place at runtime. It is a process in which a call to an overridden method
is resolved at the runtime rather than compile time. In this process, we use
the reference variable of the superclass to call the overridden method. Hence,
we can see that Runtime polymorphism will occur when we are overriding any
method during inheritance (parent-child/class relation).
Before going
through Runtime polymorphism let us understand a small yet important term “Upcasting”.
Upcasting is when a parent class reference variable holds a child class object.
Possible combinations of creating an object
Let’s see what
are the possible combinations that we can use to create the object in case of Parent-Child
relationship. What reference type can holds which type of object. Consider the Parent
and Child class.
Keeping these
classes in mind, let's discuss on below Points by creating objects one by one
and see which case is correct and which case is incorrect.
Case 1: Superclass reference variable holding its Object.
Animal a = new Animal();
This line states
that “every animal is an animal”, this
statement is true.
Case 2: Sub-class
reference variable holding its object
Tiger t = new Tiger();
This line states
that “Every tiger is a tiger”, this
statement is also true.
Case 3: Superclass reference holding the object of sub-class.
Animal a = new Tiger();
This line states
that “Every tiger is an animal”,
this statement is also true.
Case 4: Child class reference variable holding parent class
object.
Tiger t = new Animal();
This line states
that “Every animal is a tiger”, this
statement is false. Hence, this cannot
happen.
From the
above discussion we can say that:
A Child class reference variable cannot hold the parent class object.
If this small concept is clear then we are good to move ahead. Now let’s directly jump into some examples where we can see Runtime polymorphism.
Example Java runtime Polymorphism:
In this example, we will take a person class. This class will act as the superclass (parent class). There will be two more classes which will act as sub-class (child class). These classes will be Student class and Employee Class. These classes will extend Person class:
}
Output:
He is an
Employee;
In this example,
we can see that since method invocation is determined by JVM hence it is
Runtime Polymorphism.
Runtime Polymorphism with data members
In this example,
we will take the same class, but this time instead of defining the methods we
will take a look at whether a data member will be overridden or not.
Output:
20.
From this
example we can conclude that a method is overridden, no method overloading takes
place in data members.
Related Articles:
1.
Core java interview questions.
2.
Java 8 interview questions.
3.
Internal working of Hashmap in java.
0 Comments