Updated April 5, 2023
Introduction to Polymorphism in OOPS
Polymorphism is the one of the most important concepts of OOPS, which is an object-oriented programming language. By the use of Polymorphism, we can have different or multiple forms of the methods, variable or object. Moreover, by the use of Polymorphism, we can give different implementations to the same method according to the need of the class.
This concept brings us the scalability and increases the code’s readability, and by using this, we can have our own implementation for the same method in Parent class. In general, it can be achieved with the help of inheritance that means class should belong to the same hierarchy tree. Here we will see more about the implementation of Polymorphism in the programming world and how we can access using the object of the class.
Syntax of Polymorphism in OOPS
As we have already known that Polymorphism, is a concept from OOPS, which can be used to provide the multiple forms of a method.
Here we will see the syntax for this to achieve in the Java language.
public class classs_name{
public void method_name(){
// logic goes here ..
}
}
public classs_name extends other_class{
@Override
public void method_name(){
// logic goes here ..
}
}
In this way, we can achieve Polymorphism in OOPS; we can see from the above syntax that we are trying to achieve Polymorphism, in which we are providing multiple implementations for a specific method.
Example:
public class Parent{
public void method_name(){
// logic goes here ..
}
}
public childDemo extends Parent{
@Override
public void method_name(){
// logic goes here ..
}
}
How to Perform Polymorphism in OOPS?
It is very important because it provided the multiple forms to the methods, objects and variables. By the use of it, we can increase the readability of the code as well; it is easy to develop and understand. In OOPS, we can achieve it in two ways, and they are often categorized as compile-time polymorphism and runtime polymorphism. In object-oriented programming languages like Java, we have method overloading and method overriding to implement it.
1. Compile Time Polymorphism
In OOPS, we can achieve compile-time polymorphism with the help of method overloading; in this, java identified which method to call by checking their method signature. In method overloading, we should have the same method name but with a different number of arguments and their return type any of them to implement this. In short, when the object is bound to its functionality, the compile-time than this type of form is known as compile time. Here we will see at the syntax to achieve this in Java.
Syntax:
public class Demo {
public static int subtract(int x, int y)
{
// logic goes here .//
}
// Second addition function
public static int subtract(int x, int y, int z)
{
// logic goes here .//
}
}
In the above syntax, we are trying to implement the compile-time by the use of method overloading; if you can see, we have two methods with the same name by their signature is different. So whenever we call these methods by the use of its object, then the compiler will identify the method by checking their method signature, and this identification will be done at compiler time only. Hence this is called as compiler time. By keeping in mind the number of parameters should be different, or the parameter’s data type should be different to implement this properly.
2. Run Time Polymorphism
In OOPS, we can achieve run time polymorphism by the use of method overriding, which means it should be achieved when the two-class have some relationship mostly it is inheritance. In order to achieve this, we used one keyword named as ‘Override’; the method should be annotated with this annotation. Run time polymorphism is also known as the dynamic polymorphism because a method to call is determined at run time rather than at compiler time. However, because both methods will have the same signature and can be called using any of the class objects, the compile-time compiler does not know about this; this can only be identified at runtime.
Syntax:
public class Demo{
public void getm(){
// logic goes here .//
}
}
public class DemoEx extends Demo{
@Override
public void getm(){
// logic goes here .//
}
}
As you can see in the above syntax, we are trying to implement the runtime by the use of method overriding. In this, we should have the same method in both the class that is with the same signature and but we have provided to provide a different implementation for each class we override this. In this way, we achieve runtime in OOPS. Also, at the time of calling the methods, we can class this by using the parent or child object; it totally depends on how we write our logic.
Example of Polymorphism in OOPS
A simple example to show polymorphism in OOPS.
Code:
Shape class:
public class ShapeDemo {
public void getShape(){
System.out.println("I am shape class !!");
}
}
Circle class:
public class CircleShape extends ShapeDemo{
@Override
public void getShape() {
System.out.println("I am shape circle !!");
}
}
Test class:
public class ShapeTest {
public static void main(String[] args) {
ShapeDemo shapeDemo = new ShapeDemo();
CircleShape circleShape = new CircleShape();
shapeDemo.getShape();
circleShape.getShape();
}
}
Output:
Conclusion
Polymorphism is very important in OOPS, which helps us to provide us with the multiple forms of the variable, object and methods, which increase the reusable and readability of the code; also, we can have different implementations for the same method. It is easy to understand and implement as well.
Recommended Articles
We hope that this EDUCBA information on “Polymorphism in OOPS” was beneficial to you. You can view EDUCBA’s recommended articles for more information.