Polymorphism: Introduction
Definition
Polymorphism is the ability of one interface, method, or operator to take many forms and perform different actions based on the object, type, or context in which it is used.
In object-oriented programming, polymorphism allows a parent class reference to refer to objects of different child classes, and the correct method implementation is chosen automatically at runtime or compile time, depending on the type of polymorphism.
Main Content
1. Basic Meaning of Polymorphism
- The word polymorphism comes from two Greek words:
polymeaning many andmorphmeaning form. - It describes the ability of a single name or operation to represent multiple behaviors in different situations.
Polymorphism is not limited to programming; it is also seen in real life. For example, a person can act differently as a student, employee, or parent. Similarly, in programming, one method name can represent different actions depending on the object.
Example idea:
print()may print text, numbers, or objects.area()may calculate area for different shapes using the same method name.
This concept helps in:
- Writing cleaner code
- Reusing existing code
- Designing systems that can grow easily
2. Types of Polymorphism
Compile-time polymorphism
- : The decision about which method to call is made during compilation. This is commonly achieved through method overloading or operator overloading.
Run-time polymorphism
- : The decision is made during program execution. This is commonly achieved through method overriding and dynamic method dispatch.
Compile-time polymorphism:
- Same method name, different parameter lists
- Example:
add(int a, int b)andadd(float a, float b)
Run-time polymorphism:
- Same method signature in parent and child classes
- Child class overrides parent method
- Example:
Animal sound()called onDogandCatobjects producing different outputs
These two forms are the foundation of polymorphism in object-oriented programming.
3. Polymorphism Through Inheritance and Interfaces
- Inheritance allows a child class to reuse and extend the behavior of a parent class.
- Interfaces define a common contract that multiple classes can follow, even if their internal implementations are different.
Inheritance-based polymorphism:
- A base class can define a general behavior.
- Derived classes provide specific versions of that behavior.
Interface-based polymorphism:
- Multiple unrelated classes can implement the same interface.
- The interface name can be used to refer to objects of any implementing class.
This is very useful when building large systems because:
- New classes can be added without changing existing code much
- Code can work with objects through a common type
- It supports loose coupling and modular design
Working / Process
- A common parent class, interface, or method name is defined.
- Different child classes or implementations provide their own versions of the behavior.
- When the method is called, the correct version is selected based on the object type or the method signature.
For better understanding, consider this simple flow:
Animal
|
-----------------
| |
Dog Cat
Animal sound() -> Dog sound() = Bark
Animal sound() -> Cat sound() = Meow
Process explanation:
- A general type like
Animalis created. - Specific classes like
DogandCatinherit or implement from it. - The same call
sound()behaves differently depending on the actual object.
For compile-time polymorphism:
- The compiler checks the method name and parameters.
- It selects the correct overloaded method before execution.
- The method runs according to the matched signature.
For run-time polymorphism:
- The program creates an object of a child class.
- The object is referenced by a parent class variable or interface type.
- At runtime, the overridden child method is executed instead of the parent version.
Example:
Animal a = new Dog();a.sound();callsDog’s version ofsound()
This dynamic selection is what makes polymorphism powerful and flexible.
Advantages / Applications
- It improves code reusability by allowing common code to work with many different types.
- It increases flexibility because new classes can be added with minimal changes to existing code.
- It simplifies maintenance by reducing repeated code and making programs easier to extend.
- It supports real-world modeling, such as shapes, vehicles, employees, or animals having different behaviors.
- It is widely used in frameworks, libraries, APIs, graphical user interfaces, and database systems.
- It helps achieve loose coupling, which means code depends on general behavior rather than specific implementations.
- It allows better scalability in large software projects.
Common applications include:
- Shape calculations such as
area()forCircle,Square, andTriangle - Payment systems such as
pay()for credit card, UPI, or cash - Notification systems such as
send()for email, SMS, or push notifications - Game development where different characters respond differently to the same action
- GUI event handling where buttons, menus, and checkboxes handle events in different ways
Summary
- Polymorphism means one interface or method can behave in many forms.
- It is a core object-oriented concept used to improve flexibility and reuse.
- Important terms to remember: polymorphism, overloading, overriding, inheritance, interface, runtime dispatch