A POCKET GUIDE TO OBJECT-ORIENTED PROGRAMMING (OOP)

Polymorphism

Polymorphism is the most difficult, but also an interesting concept of Object-Oriented Programming. This word is basically a combination of two greek words, where ‘Poly’ means ‘Many’, and ‘Morphos’ means ‘form’.

Do you remember one of my favourite movie ‘The Matrix’? Here is my little secret, that I am finally revealing here, related to this movie. In this movie we have a character or villain known as ‘The Agent’. An ‘Agent’ can shape itself into any other human being that it can touch or make contact with. And I always use this character to remember definition of ‘Polymorphism’. As just like an agent in Matrix …

Polymorphism is the ability of an entity (where entity can be anything a class, object, method, property or anything else) to take on different meaning in different context or space for another entity.

I know this might be little Confusing at this time. Let’s take an example to better understand this. Let’s assume our ‘Person’ class from our Learning Management System in our previous chapter of ‘Inheritance‘ has a simple function/method named ‘printContact()’. This function has a function body like this in class ‘Person’ in java.

// Let's assume it just return
// name value of an instance
// variable of a Person object
public class Person {
    public String printContact() {
        return name;
    }
}

But, our derived classes including Student, Teacher and Staff want to print contact little differently. For example, Student contact should include roll number in it, Teacher contact should include subject, and Staff contact should include designation and department in addition to name. So you can easily identify the problem here that all three derived classes want to print contact differently. And this problem can be easily solved using concept of Polymorphism. We can just write a function with same name ‘printContact()’ and zero parameters in all three derived classes to print or return contact differently as per context of each derived class. This is how our functions might look like in all three classes.

// Please note:
// extends is a java keyword that we
// use to extend a base class or in
// other words to inherit from a
// base class.

public class Student extends Person {
    public String printContact() {
        return name + " (" + rollNo + ")";
    }
}

public class Teacher extends Person {
    public String printContact() {
        return name + " (" + subject + ")";
    }
}

public class Staff extends Person {
    public String printContact() {
        return name + " (" + department + " - " + designation + ")";
    }
}

This concept of writing a function is also known as function or method overriding. Where a derived class can provide different body or logic for a function in base class. And it is just one type of Polymorphism.

As different languages provide different types of Polymorphism, but we are not discussing any specific programming languages here. That’s why I don’t want to go in details of all different types of Polymorphism. But, you should google or read a book specific to your preferred programming languages to get better understanding of available types of Polymorphism in it.

I hope all this explanation have helped you to understand the basic concept of ‘Polymorphism’. But, if you still have any confusion on this, please ask your questions as comments below.

Let’s move on to our next chapter. Please click on ‘Next’ to continue.

Image placeholder

Hi! I'm Zeeshan Elahi

I have compiled and prepared this content with love for people like me and you. And as you know it always take some time and extra cups of coffee to read, compile and prepare a course or manual. If you have like reading this content and want to say thanks, please consider supporting from more stuff like this.

Leave a comment

Your email address will not be published. Required fields are marked *

*

*