In this article, you will be learning Polymorphism in Java Programming Language. Polymorphism simply means “having many/multiple forms”. It is one of the feature of object oriented programming.
In Java, the use of the method of a parent class in one or more child classes with the same method names for different purposes is known as Polymorphism. For Example, the fee of three different courses in W3Training School is different but the method used is same for all three courses.
There are two features which defines Polymorphism more clearly, these are Method Overriding and Method Overloading. These features can be bring into use by creating subclasses that inherit properties of a super class.
Thus, we can say polymorphism and inheritance are correlated to each other. The above image shows an example of hierarchical inheritance with method overriding.
Also Read how to Modifiers Used In Java Programming Language
TYPES OF POLYMORPHISM IN JAVA
Based on the above features i.e, Method Overriding and Method Overloading, polymorphism is divided into two parts which are described below in detail.
- Run-time Polymorphism (Method Overriding)
- Compile-time Polymorphism (Method Overloading)
[1] RUN-TIME POLYMORPHISM
In this type of polymorphism, the JVM resolves a call to a method at run-time. Method Overriding better defines the run-time polymorphism in which the same method name is utilized in the parent class and sub-classes having the same parameters(arguments).
It is also known as Dynamic Polymorphism due to its dynamic binding/ late binding characteristic.
Example: We have shown below an example of Run-time Polymorphism/ Overridingused in java programming language.
class W3TrainingSchool {
public int courseFee () {
return(0) ;
}
}
class DigitalMarketing extends W3TrainingSchool {
public int courseFee () {
return(50000) ;
}
}
class PHP extends W3TrainingSchool {
public int courseFee () {
return(30000) ;
}
}
class WebDesigning extends W3TrainingSchool {
public int courseFee () {
return(45000) ;
}
}
class RuntimePolymorphism {
public static void main(String args[]) {
W3TrainingSchool obj1 = new DigitalMarketing ( ) ;
System.out.println(“Fee of Digital Marketing =” + obj1.courseFee ( )) ;
W3TrainingSchool obj2 = new PHP ( ) ;
System.out.println(“Fee of PHP =” + obj2.courseFee ( )) ;
W3TrainingSchool obj3 = new WebDesigning ( ) ;
System.out.println(“Fee of Web Designing =” + obj3.courseFee ( )) ;
}
}
Output:
Fee of Digital Marketing = 50000
Fee of PHP = 30000
Fee of Web Designing = 45000
Explanation:
Clearly, the parent class W3TrainingSchool holds the object of its child classes DigitalMarketing, PHP and WebDesigning. Furthermore, same method courseFee() is used in the parent class and its three child classes having different return values.
[2] COMPILE-TIME POLYMORPHISM
In this type of polymorphism, the compiler resolves a call to a method at compile-time rather than resolving it at run-time. Method Overloading better defines compile-time polymorphism.
Like run-time polymorphism, it also utilizes same method name in the parent class and its child class. The only difference is that different parameters are used in the method.
Also Check:- Types of Variables in Java Programming Language
It is also known as Static Polymorphism due to its static binding/ early binding characteristic.
Example: We have shown below an example of Compile-time Polymorphism/ Overloading used in java programming language.
class Division1 {
static int divide (int x, int y) {
return x/y ;
}
}
class Division2 {
static float divide ( float x, float y ) {
return x/y ;
}
}
class CompileTimePolymorphism {
public static void main (String args[]) {
System.out.println(Division1.divide(30,20)) ;
System.out.println(Division2.divide(30,20)) ;
}
}
Output:
1
1.5
Also Check: Inheritance in Java Programming Language
Explanation:
In the above example, the method names used in two classes Division1 and Division2 are same but they have different parameters int and float respectively.
Also Read
We have provided you the description on Polymorphism and its types in Java Programming language. Hope you like this post. For more updates and related information, stay connected to our blogs on java programming language.