Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is polymorphism?
3.
Types of Polymorphism
3.1.
Compile-Time Polymorphism
3.2.
Runtime Polymorphism
4.
Practical Applications of Polymorphism
5.
Frequently Asked Questions
5.1.
What is polymorphism in OOP?
5.2.
What are the types of polymorphism?
5.3.
Why is polymorphism important?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

What is polymorphism?

Author Nikunj Goel
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Polymorphism is derived from Greek that means "multiple forms." Excluding computer programming, the idea of polymorphism occurs in other universal areas, which includes chemistry, biology, and drug development.

This article will demystify polymorphism, providing practical examples to help enhance your understanding.

 what is Polymorphism?

What is polymorphism?

Polymorphism is a Greek word that means "many shapes." In programming, it's a principle that allows objects to take on many forms, making it possible to use a single interface to represent different types of entities. It helps in making our programs more modular and scalable, reducing complexity and increasing efficiency.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Types of Polymorphism

Polymorphism in programming is mainly classified into two types: compile-time (or static) and runtime (or dynamic).

Compile-Time Polymorphism

Runtime Polymorphism

Compile-Time Polymorphism

Also known as static polymorphism, it is achieved by function overloading or operator overloading.

In function overloading, two or more functions can have the same name but different parameters. The compiler decides which function to use based on the function signature (name and number/type of parameters).

Here's a simple example in Java:

class Demo {
    void display(int a) {
        System.out.println("Displaying: " + a);
    }
    void display(int a, int b) {
        System.out.println("Displaying: " + a + " and " + b);
    }
}

 

In this code, the display() function is overloaded with two different parameter lists.

Runtime Polymorphism

Known as dynamic polymorphism, it is implemented using inheritance and interfaces. Runtime polymorphism is achieved by method overriding, where a subclass provides a specific implementation of a method that is already present in its parent class. The correct method is selected at runtime based on the actual object.

Here's a simple example in Python:

class Animal:
    def sound(self):
        print("An animal makes a sound")
class Dog(Animal):
    def sound(self):
        print("A dog barks")
animal = Animal()
animal.sound()
dog = Dog()
dog.sound()

 

In this example, the sound() method in the Dog class overrides the same method in the Animal class.

Practical Applications of Polymorphism

Polymorphism is at the heart of many fundamental design patterns in OOP, like Strategy, State, and Template Method patterns. It's used in various real-world applications, from creating UI controls to designing complex software systems.

Frequently Asked Questions

What is polymorphism in OOP?

Polymorphism is an OOP principle that allows objects to take on many forms. It enables a single interface to represent different types of entities.

What are the types of polymorphism?

The two main types of polymorphism are compile-time (or static) and runtime (or dynamic) polymorphism.

Why is polymorphism important?

Polymorphism makes programs more flexible and scalable. It reduces complexity by allowing the same interface to be used for different types, thus promoting code reusability and clean, modular design.

Conclusion

Polymorphism is a powerful principle in object-oriented programming, enhancing the flexibility and maintainability of code. Understanding and correctly implementing polymorphism can significantly elevate the quality of your software design. Remember, the beauty of polymorphism lies in its ability to simplify complex structures by letting a single interface represent various entities. Happy coding!

Previous article
Four Pillars of Object-Oriented Programming (OOPS)
Next article
Difference Between Compile Time and Run Time Polymorphism in Java
Live masterclass