A SIMPLIFIED STATEMENT ON POLYMORPHISM

Why and when to use polymorphism?

Object-Oriented Programming or shortly, OOP, is a constitutional part of modern application architecture. OOP comprises four fundamental concepts: A PIE: Abstraction, Polymorphism, Inheritance, and Encapsulation.

Hatef Palizgar
Published in
4 min readDec 12, 2019

--

Polymorphism is made of two parts: poly (multiple) and morph(form). According to Wikipedia.com, polymorphism is described as follows:

In programming languages and type theory, polymorphism is the provision of a single interface to entities of different types or the use of a single symbol to represent multiple different types.

To make it simpler:

In the world of OOP and objects, every single object that has more than one shape or can be assigned to more than one class is called a polymorphic object.

As a Java trainer, I continually come across various questions like “what is polymorphism exactly?” or “why do we need such thing at all?”.

Since understanding polymorphism is essential to grasp the idea behind object-oriented programming, I decided to write this article. Indeed, what made me write on this topic was a question that one of my students asked me one day:

Hey Hatef !! Why we need polymorphism? What is the point in it?

The rest of this article is the lines I wrote to describe why and when to use polymorphism.

The main idea for the polymorphism is to bring flexibility. How? I’ll describe it with Car Service Shop example:

Photo by Davids Kokainis on Unsplash

Let’s imagine you are an experienced car technician at a service shop that can repair any B-class car. If you put this concept in OOP format, you have to create a class called Technician as below:

class Technician{
private String name;
private Car car

public Technician(String name){
this.name = name;
}

/*
... getters and setters
*/


public void repair(Car theCar){
// ... the body of repair method
}
}

So far, so good. We have created out Technician class with one method repair() that accepts any object of type Car.

The point is here:

The shop manager knows that you can repair any B-class car. Thus, if any customer with car models below comes to the shop and asks for a repair job, the manager is confident that you can repair them successfully.

In OOP terms, the manager can call you (instantiate a Technician object) and ask you to repair a car (call you repair() method ). But wait, which type of car the manager is going to pass as you theCar argument?

As a technician you are not worried about that. Because you can repair any sort of B-class car. As a result the codes below are all true and make sense:

Technician hatef = new Technician("Hatef");// create several Car instancesCar yaris = new Toyota();
Car focus = new Ford();
Car azera = new Hyundai();
Renault megane = new Renault();
// create manager objectManager manager = new Manager();// now manager calls the technician to start repairinghatef.repair(yaris); // hatef is able to repair any Car instances
hatef.repair(focus); // focus is a Car instance
hatef.repair(azera); // azera is a Car instance
// now, watch below code, it is wrong because of polymorphism violation. megane is not an object of Car class.hatef.repair(megane);

But wait, why it is like so? why the code on last line is not right?

Answer:

Due to polymorphism objects like yaris, focus, azera are all reference variables of type Car that point to an object of different type. yaris points to a Toyota. focus points to a Ford and azera points to a Hyundai.

BUT: megane is a reference variable of type Renault that points to a Renault object, so it is wrong to say: megane is a car !!!!

So, the point here is that if you have noticed, we were able to process several objects of a different type into a single Car reference variable. This way, it made coding the repair() method very easy for us. We just needed to say public void repair(Car theCar){....}.

Consider we had no polymorphism. How you could write the repair method in its simplest form ??

With no polymorphism, you have to re-write the Technician repair() method as below:

public void repair(Toyota theCar){....}// an overloaded repair
public void repair(Ford anotherCar){...}
//another overloaded repair
public void repair(Hyundai anotherCar){...}

Did you notice? Instead of a simple line of code, I have to write a bunch of overloaded methods.

This is the magic of POLYMORPHISM

Now, let me put some other concepts here:

Just think about the situation that you have finished writing your code and built your app. It is now the day you have to release the product to the customer. Suddenly, the customer comes to you and says:

Hey Hatef! Did you know that we have just launched our new service department? We are now giving service to heavy vehicles like bus and tractors :D

OOPS!! I can’t believe it. I have not considered this change in customer requirements. My code is only able to repair Ford, Toyota, and Hyundai.

So, try to hug Polymorphism. Try to love it and use it in your daily coding tasks. Otherwise, you have to re-invent the wheel each time.

For more information, take a look at this great post by Shanika Ediriweera:

--

--