Polymorphism in Java
Java has different features in it. They are inheritance, polymorphism, data abstraction, encapsulation, etc. As we can see, polymorphism is one of them.
Polymorphism is made up of two Greek words, “poly” and “morphs”. “poly” means many and “morphs” means forms. Therefore, polymorphism means the ability to exist in different forms. A real-life example of polymorphism is that one word can be used differently.
Consider the word spring. In one form, it is a season, and in the other form, it means a metal coil. Consider the other example, well. On the one hand, well depicts positivity, and on the other hand, it means a structure dug in the ground for storing water.
In java, polymorphism means the ability to perform the same task differently. For example, we can use inheritance when many classes are linked by inheritance. Inheritance is the property of an Object-Oriented Programming Language in java that helps create a new class from an existing class. By doing this, the new class created can inherit the attributes and methods from the existing class. Now, polymorphism uses these attributes and methods to perform different tasks.
In java, polymorphism is of two types. These are compiled time and run time polymorphisms.
Compile Time Polymorphism
It is also known by the terms static polymorphism, early binding, or overloading. Finally, compile-time polymorphism is a process in which the method is called compile time.
Method Overloading is an act of implementing compile-time polymorphism. Other examples of compile-time polymorphism are Constructor Overloading and Method Hiding.
Method Overloading
Method Overloading is a process for implementing compile-time polymorphism in which the methods have the same name but possess different parameters. In this way, the same command will be displayed in different ways.
Different ways to carry out Method Overloading are:
- First, by changing the number of parameters: The class name remains the same in this type of overloading, but the parameters taken as inputs are varied
- By changing the data types of parameters: In this type of overloading, the class name is kept the same, but the data types of the parameters taken as input are varied
Runtime Polymorphism
It is also known as dynamic polymorphism, late binding, or overriding. In this type of polymorphism, the method is called during the run time. Method Overriding is used for the implementation of run time polymorphism. In this, a child class inherits the data of the parent class. Both classes possess the same methods, but implementation is different.
Difference Between Compile Time and Run Time Polymorphism
Compile Time Polymorphism |
Runtime Polymorphism |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Conclusion
This article first provides a basic explanation for the polymorphism and then explains the compile-time and runtime polymorphism and then the difference between the two.
By going through this article, one can easily explain the key differences between the compile-time and runtime polymorphism.