Polymorphism in Oops: Polymorphism is one of the most important principles of object-oriented programming. ‘Poly’ means many, and ‘morphism’ means forms. Hence, polymorphism means many forms. An entity that can exist in multiple forms brings polymorphism in object-oriented programming. In this article, we will learn about this oops principle in detail.
Polymorphism in Object-Oriented Programming
Polymorphism in OOPs is one of the four important principles of object-oriented programming (OOP), which allows objects of different classes, objects, variables and methods to exist in different forms. With the help of polymorphism in oops, it is easy to represent one single form into various forms.
There are two main types of polymorphism in object-oriented programming. These are compile-time polymorphism and Run-time polymorphism. Polymorphism allows flexibility and increases efficiency while designing software systems. We can add new classes easily with the help of polymorphism without modifying existing entities.
Types of Polymorphism in OOPs
There are two major types of polymorphism in oops. Let us learn about them one by one.
1. Compile time Polymorphism
Compile time polymorphism is a type of polymorphism occurring when the method call gets resolved during compile time. Compile time polymorphism is achieved with the help of method overloading and operator overloading.
In method overloading, we use the same methods with different parameters to achieve polymorphism. We do not need to keep different names for the same function. This improves the effectiveness of the code. It is also known as Static binding or early binding. Let us suppose we need to multiply two different numbers, and we can write a function name multiply to achieve this. However, if we need to multiply three numbers, then with the help of polymorphism in oops, we do not need to write the complete code again.
Polymorphism in Oops |
public int multiply(int a, int b) {
int prod = a * b; return prod; } public int multiply(int a, int b, int c) { int prod = a * b * c; return prod; } }
// Class 2 // Main class class Operation { // Main driver method public static void main(String[] args) { Product ob = new Product();
int prod1 = ob.multiply(1, 2);
System.out.println( “Product of the two integer value :” + prod1);
// Calling method to multiply 3 numbers int prod2 = ob.multiply(1, 2, 3);
// Printing product of 3 numbers System.out.println( “Product of the three integer value:” + prod2); } } |
Here, we are using multiply(int a, int b) for two-digit multiplication. For three-digit multiplication, we call the method with the same name but a different parameter.
Also check: Abstraction in OOPs
2. Run time Polymorphism
Run time polymorphism is a type of polymorphism in OOPS which is resolved during runtime. This is generally achieved with the help of method overriding. Method overriding is used to implement a method that is already defined in its parent class or super class. Runtime polymorphism in oops is also known as dynamic binding or late binding.
Let us understand runtime polymorphism with the help of an example given here in the table.
Polymorphism in Oops |
class Animal {
void sound() { System.out.println(“Animal makes a sound”); } } class Dog extends Animal { void sound() { System.out.println(“Dog barks”); } } |
In this example, the sound method, which is already defined in the Animal class, is overridden and used in the Dog class. When this sound method is called for a dog object, it will print “Dog barks”.
Recommended Reads
Data Science Interview Questions and Answers
Data Science Internship Programs
IIT Madras Data Science Course
Example of Polymorphism in OOPs
We can understand polymorphism in OOPs with the help of many real-world examples. Let us check out some of these examples below.
- A woman is a mother to her son. She is a teacher for the other and the wife of his husband.
- A man is a labourer at work, a father at home and a shop buyer.
- A media player can play different video, audio, etc. With the help of polymorphism, we can create a superclass for all media files and use it to perform various functions.
Advantages of Polymorphism in OOPs
Polymorphism in Oops has many advantages. Let us know about the major benefits in detail.
- Polymorphism allows extensibility to the developers. With the help of polymorphism in OOPs, we can easily create a new class derived from the existing class.
- The code written can be used without modification. It helps to reduce the redundancy in code and also removes the need to write similar code repeatedly.
- Polymorphism provides method overloading and method overriding.
- It allows better readability and code maintenance.
- Programmers are able to write codes more frequently and creatively.
- All the major data types can be stored in a single variable with the help of polymorphism which makes the implementation easy to use and develop.
- It allows for simple debugging.
Also check: Encapsulation in OOPs
Difference Between Compile Time And Runtime Polymorphism
Polymorphism in OOPs |
|
Compile time polymorphism in OOPs | Runtime polymorphism in OOPs |
Compile time polymorphism is also known as static binding or early binding. | It is also known as Dynamic polymorphism or late binding. |
Static polymorphism can be attained when the compiler resolves method calls. | In runtime polymorphism, the method calls are not resolved by the compiler. |
Compile time polymorphism can be attained by method overloading. | Runtime polymorphism can be attained during runtime. |
Method overloading is a type of polymorphism in which one method can have the same name with different parameters or return type. | Method overriding is the runtime polymorphism that implements a method already defined by its parent class or super class |
It is known as early binding because it takes less time to execute. | It takes more time to execute the method, hence known as late binding. |
No inheritance is present. | Inheritance is present. |
Recommended Reads | |
Java Vs C++ Vs Python | Java or C++ which is better |
Linear Search in C | what are arrays in C? |
Polymorphism in OOPs FAQs
Q1. What is Polymorphism in OOPs?
Ans: Polymorphism in OOPs is one of the four important principles of object-oriented programming (OOP), which allows objects of different classes, objects, variables and methods to exist in different forms.
Q2. How many types of polymorphism are there in object-oriented programming?
Ans: There are two major types of polymorphism in oops, compile time polymorphism and run time polymorphism. Know about them in detail in this article.
Q3. What is the use of polymorphism in OOPs?
Ans: Polymorphism in OOPs helps to use overriding, overloading, inheritance in our code which makes it more readable, effective and faster.