Java Runtime Polymorphism - The Coding Shala
Home >> Learn Java >> Java Runtime Polymorphism
Java Runtime Polymorphism
Polymorphism means many forms. There are two types of polymorphism in Java:
- compile-time polymorphism
- Runtime polymorphism
Runtime polymorphism is also known as Dynamic Method Dispatch. Runtime polymorphism is a process in which a call to an overridden method is resolved at runtime, not at the compile-time.
An overridden method is called through the reference variable of a superclass. We use upcasting for runtime polymorphism.
If the reference variable of Parent class refers to the object of Child class, it is known as upcasting.
The following code shows how to achieved runtime polymorphism using upcasting.
Example of Java Runtime Polymorphism
This following example explains java runtime polymorphism:
Java Runtime Polymorphism with Data Member
In Java, we can override a method but not the data members so runtime polymorphism can not be achieved by data members. The following example explains it:
Other Posts You May Like