A Peek into Polymorphism in OOP

Rashmi Sandamini
3 min readJan 21, 2024

--

Polymorphism is one of the most important concepts and having a better understanding about polymorphism would come in handy when designing the software. Imagine a world where a single method name can take on multiple forms, adapting itself to different situations… In this article we’ll explore ‘Polymorphism’, one of the core concepts in OOP.

What is Polymorphism?

As the name itself implies, “poly” means “many” and “morph” refers to “forms”. So, the polymorphism simply implies one property having many forms.

In OOP, polymorphism simply refers to the capability of a variable, function or object to take on different forms.

Let’s take this simple example to understand what this means. Picture an actor on a stage. When they wear a lab coat, they’re a smart scientist doing experiments. In a police uniform, they turn into a watchful officer, and with a chef’s hat, they become a great cook. It’s like having one actor play many parts, adapting to different situations. This shows how something or someone can be really versatile, changing roles to fit in wherever they are.

Just like our actor effortlessly transforms roles on stage, polymorphism in programming comes in different types. Let’s explore the various types that bring this adaptability to our code.

Types of Polymorphism

Polymorphism in OOP can be divided into two types. They are,

  • Static Polymorphism (Compile Time Polymorphism)
  • Dynamic Polymorphism (Run Time Polymorphism)

Static Polymorphism (Compile Time Polymorphism)

To achieve the static polymorphism we can use method overloading. Which means we can define multiple methods within the same class, each with a different set of parameters. The key point here is that these methods share the same name but have distinct parameter lists.

Let’s look at a simple example in Java to understand the static polymorphism.

public class Printer {
public static void printValue(int num){
System.out.println("Printing integer: "+num);
}

public static void printValue(double num){
System.out.println("Printing double: "+num);
}

public static void printValue(String str){
System.out.println("Printing string: "+str);
}

public static void main(String[] args) {
printValue(5); // Calls the printValue(int num) method
printValue(5.5); // Calls the printValue(double num) method
printValue("Polymorphism"); // Calls the printValue(String str) method
}
}

Output:

Printing integer: 5
Printing double: 5.5
Printing string: Polymorphism

In the above example, the Printer class has multiple printValue methods with different parameter types (int, double, and String). When we call the printValue method, the compiler determines which version of the method to invoke based on the argument type we provide.

Then the appropriate printValue method is selected at compile time based on the argument type, demonstrating static polymorphism through method overloading.

Dynamic Polymorphism (Run Time Polymorphism)

To achieve the dynamic polymorphism we can use method overriding, which takes inheritance into account. When a subclass provides a specific implementation for a method that is already defined in its superclass, it’s called method overriding. The overridden method in the subclass should have the same method signature as the one in the superclass.

Let’s look at an example to understand this behavior.

public class Animal {
public void makeSound() {
System.out.println("Some generic sound");
}
}

class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Bark! Bark!");
}
}

class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Meow!");
}
}

class Demo{
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();

dog.makeSound();
cat.makeSound();
}
}

Output:

Bark! Bark!
Meow!

In this example, the Animal class has a method makeSound(), and both Dog and Cat classes override this method with their specific sound implementations. The actual method that gets executed is determined at runtime based on the type of the object.

We have reached the end of this article, we’ve explored the concept of polymorphism, understanding both dynamic and static polymorphism and how to achieve them through method overriding and method overloading. These principles play a crucial role in shaping the flexibility and adaptability of object-oriented programs. I hope this will help you to understand the concepts. Let’s meet in another article… 😊.

--

--