Skip to main content

Polymorphism in OOPs: Definition, Types, and Real-Life Examples

 


What is Polymorphism in OOPs?

The word Polymorphism is derived from two Greek words:

  • Poly → many

  • Morph → forms

So, Polymorphism means having many forms.

In computer programming, polymorphism refers to the property that a function, operator or object will behave differently under different circumstances. That is, the input can be different tasks done by the same name of the function.

Simple Analogy:
Think of the word “run”:

  • Athletes run in a race.

  • Programs run on a computer.

  • Cars run on fuel.

It is repetition of the same word, but the meaning is different depending on the context. This is the polymorphism of OOP.


Types of Polymorphism in C++

The two most common types of polymorphism in C++ are:

  1. Compile-Time Polymorphism (Static Polymorphism)

  2. Run-Time Polymorphism (Dynamic Polymorphism)

Let us get to know them by example.


1. Compile-Time Polymorphism (Static Polymorphism)

This type of polymorphism is resolved at compile time.
It is achieved using:

  • Function Overloading

  • Operator Overloading

(a) Function Overloading 

In C++, Function Overloading is a feature, which enables you to create several functions of the same name but with different parameter lists (different number or different type of parameters).

At compile time, the compiler will identify the function to be called, based on the arguments being passed.That is why this is an example of Compile-Time Polymorphism.

Rules of Function Overloading in C++

  1. Functions must have the same name.

  2. They must differ in either:

    • Number of parameters

    • Type of parameters

    • Or both

  3. The return type alone cannot distinguish overloaded functions.

Real-Life Analogy

Just imagine functional overloading as an ATM machine:

  • On inserting a debit card, it will be affected like a withdrawal.

  • When you insert a credit card, it enables credit transaction.

  • If a passbook is inserted, it prints out.

Depending on the type of input on which the type of input is used, the action (function) remains the same (machine name is the same).


Key Points

  • Function overloading is solved at compile time (therefore static polymorphism).

  • Improves Ease of reading (similar to similar operations).

  • Can not overload functions with the return type only.

In short:
Function Overloading = Same function name + Different parameters → Different tasks


(b) Operator Overloading 

The C++ feature of Operator Overloading gives users of C++ the ability to reassign the meaning of operators (such as +, -, *) to their own data types (such as classes).

This causes objects to act as inbuilt data types in operating operators.

What is the point of Operator Overloading?

C++ operators default only on primitive types of data (int, float, etc.).However, what about addition of two Complex numbers or comparing two Strings (objects)?That is where operator overloading comes in.

Code Link

Real-Life Analogy

The redefinition of a symbol is a kind of redefining the operator:

  • In mathematics + is addition of numbers.

  • In reality, the phone + sign means international dialing.

  • We can concatenate + (when using strings) or add (when using objects) in programming.


Key Points About Operator Overloading

  • The only operators that can be overloaded are the existing operators. (You can’t create new ones like $$).

  • Some operators cannot be overloaded:

    • . (dot)

    • :: (scope resolution)

    • sizeof

    • ?: (ternary operator)

  • The operator overloading will be able to make the code more reusable and readable.

In short:
Operator Overloading = redefining operators (+, -, ==, etc.) for objects, so they work like built-in types.


2. Run-Time Polymorphism (Dynamic Polymorphism)

This type of polymorphism is resolved at runtime.
It is achieved using:

  • Function Overriding

  • Virtual Functions

(a) Function Overriding

Function Overriding occurs when a derived class (child class) implements a particular implementation of a definite function already implemented in its base class (parent class).

Unlike function overloading (compile-time), function overriding is an example of run-time polymorphism, because the function to be executed is decided at runtime.


Rules of Function Overriding in C++

  1. The base and derived class functions must have the same name, same parameters, and same return type.

  2. The base class function must be marked as virtual (to achieve runtime polymorphism).

  3. The function is called through a base class pointer or reference.

Code Link


Real-Life Example

Suppose that parent class = Vehicle, and derived classes = Car, Bike, Bus.

  • The parent class has a function start().

  • Each child overrides it:

    • Car → "Car starts with a key"

    • Bike → "Bike starts with a kick"

    • Bus → "Bus starts with a button"

Although the name of the start method is similar, when invoked at runtime, its behavior varies depending on the object.

In short:
Function Overriding = redefining a base class function in a derived class → supports runtime polymorphism.

(b) Virtual Functions

A Virtual Function in C++ This is a function that is defined in a base class with the keyword virtual and is intended to be overridden in derived classes.

They guarantee that an object of the right kind is invoked at runtime, depending on the kind of object being addressed (not the kind of address).

This is the foundation of the runtime polymorphism in C++.


How Virtual Functions Work (Behind the Scenes)

  • When a class has a virtual function, the compiler creates a hidden table called V-Table (Virtual Table).

  • Each object of that class contains a hidden pointer called vptr, which points to the V-Table.

  • At runtime, the correct function is called using the vptr → V-Table mapping.

This mechanism enables dynamic dispatch (runtime polymorphism).


Key Points About Virtual Functions

  1. Stated with the keyword virtual in the base class.

  2. Overridden in the derived class by the same signature.

  3. Invoked with the help of a base class reference or pointer.

  4. When no overriding is done in a derived class, the base one is referred to.

  5. Constructors need not be virtual but destructors must be virtual (so as to prevent inheritance memory leaks).

Code Link

Real-Life Examples

Take the example of a remote control (base class pointer):

  • Together with a TV, the button activity = TV on.

  • When used together with an AC, the button action corresponding = “AC on”.

  • Fan on, when coupled with a Fan, = Fan on.

It is the same button (function call), the behavior is defined by the device (object) = That is how virtual functions work.

In short:
Virtual Functions = allow a base class pointer/reference to call the derived class function → enables runtime polymorphism.


Advantages of Polymorphism

  • Improves code reusability

  • Increases flexibility of programs

  • Makes maintenance easier

  • Supports extensibility (adding new classes without modifying existing code)


Conclusion

One of the strongest properties of OOPs is polymorphism.It enables an object, operator, or any other thing to be of various forms within a given context.

  • Function overloading and operator overloading are used to generate compile-time polymorphism.

  • Function overriding and virtual functions are used to perform run-time polymorphism.

With the use of polymorphism, it is also possible to make our code more flexible, modular, and maintainable - and this is what we need in the real-world software development.

Thanks for reading 💗!


If you found this post useful:

⮕  Share it with others who might benefit.
⮕  Leave a comment with your thoughts or questions—I’d love to hear from you.
⮕  Follow/Subscribe to the blog for more helpful guides, tips, and insights.




Comments

Popular posts from this blog

Artificial Intelligence vs Machine Learning vs Deep Learning: Key Differences Explained

  Everywhere you look today, people are talking about AI, Machine Learning, and Deep Learning . Tech companies use these terms in product launches, news headlines throw them around, and chances are you’ve already heard them in your classroom, workplace, or even casual conversations. But here’s the catch,  most people don’t actually know the difference . Some think AI, ML, and DL are the same thing. Others assume they’re just fancy names for robots or algorithms. So, what really sets them apart? Is AI just about robots? Is Machine Learning smarter than AI? And why does everyone say Deep Learning is the future? In this blog, we’ll break down introductory part of  AI, ML, and DL in simple and easy language with examples, diagrams, and real-life applications — so by the end, you’ll never be confused again. Think of Artificial Intelligence (AI), Machine Learning (ML), and Deep Learning (DL) like math sets . AI  is the  biggest set (Universal Set) ...

All About Inheritance in OOP: Explained with Real-Life Examples

  The first thing that comes to mind when  you hear the word  inheritance,  is passing something down from parents to children . In Object-Oriented Programming, it works in a very similar way—it allows one class (child class) to inherit properties and behaviors from another class (parent class). This concept makes code reusable, organized, and easier to maintain . Let’s explore inheritance step by step with real-time analogies to make it super simple. What is Inheritance in OOP? In OOP, inheritance is a concept in which a class inherits the properties (variables) and behaviors (methods) of other classes. Common features are defined in the parent class . These features are extended by the child class which is capable of adding its own features.. It is best to consider it as a family tree : a child takes after a parent in terms of eye color or height but can also possess an individual characteristic. Why Use Inheritance? Code Reusability – No need t...

Why Python Is the Best Programming Language for Machine Learning, AI, and Deep Learning

  Artificial Intelligence (AI), Machine Learning (ML), and Deep Learning (DL) have become some of the most transformative technologies of our time. From self-driving cars and recommendation systems to chatbots and healthcare diagnostics, these technologies are reshaping industries at an incredible pace. But have you ever wondered: Why do most AI researchers, data scientists, and developers prefer Python over other programming languages? In this blog, we’ll explore in depth why Python has emerged as the most popular and powerful language for AI, ML, and DL development . 1. Simplicity and Readability – Focus on Problem Solving, Not Syntax Complex mathematics and algorithms are some of the greatest challenges that newcomers in the world of AI/ML face. Python eases this load by providing a clean syntax that is easy to read. Let’s look at three concrete examples where Python’s clean syntax helps newcomers in AI/ML handle complex mathematics and algorithms more easily compared to ot...