Polymorphism
Definition
Polymorphism is a fundamental pillar of Object-Oriented Programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. The term is derived from Greek, meaning "many forms." In programming, it enables a single interface or method name to perform different actions depending on the specific object it is acting upon.
Main Content
1. Compile-Time Polymorphism (Static Binding)
- This occurs when the compiler determines which method to call during the compilation process.
- It is primarily achieved through Method Overloading, where multiple methods in the same class share the same name but differ in their parameter lists (type or number of arguments).
2. Run-Time Polymorphism (Dynamic Binding)
- This occurs when the method to be executed is determined at execution time (runtime).
- It is achieved through Method Overriding, where a subclass provides a specific implementation of a method that is already defined in its parent class.
3. Interface-Based Polymorphism
- This allows a class to implement an interface, forcing it to provide specific behaviors defined by that interface.
- It enables loose coupling, where different objects can be used interchangeably if they satisfy the same interface requirements.
Working / Process
1. Identifying the Method Signature
- The compiler or runtime environment inspects the method name and the arguments passed to it.
- If multiple versions exist, the system matches the specific call to the correct implementation based on the signature.
2. Establishing Inheritance Hierarchy
- A base class defines a virtual or overridable method.
- A derived class extends the base class and provides a custom implementation using keywords like
override.
3. Resolving the Execution Flow
- During runtime, the system uses a virtual table (vtable) to look up the correct implementation for an object.
- Even if a variable is of the base class type, the program triggers the specific code written in the actual object class.
[ Base Class: Shape ]
|
|-- [ Derived: Circle ] (draw() implementation)
|
|-- [ Derived: Square ] (draw() implementation)
Execution:
Shape s = new Circle();
s.draw(); // Calls Circle's draw, not the base Shape draw.
Advantages / Applications
- Code Reusability: Existing code can be reused by creating new subclasses that implement specific behaviors.
- Flexibility and Maintainability: You can add new classes without modifying existing, tested code, making the system easier to scale.
- Simplified Interface: Complex systems can be managed through a single interface, hiding the underlying implementation details from the user.
Summary
Polymorphism is the ability of a programming language to process objects differently based on their data type or class, allowing one interface to represent multiple underlying forms. It simplifies code structure, improves scalability, and promotes a clean design by separating the "what" from the "how."
Important terms to remember: Method Overloading, Method Overriding, Static Binding, Dynamic Binding, and Inheritance.