The word Polymorphism is derived from the Greek words poly (many) and morphs (forms). In Java, it is the ability of an object to take on many forms. Most commonly, polymorphism occurs when a parent class reference is used to refer to a child class object, allowing for dynamic and flexible code execution.
Polymorphism in Java is broadly categorized into two types, depending on when the method call is resolved by the Java Virtual Machine (JVM).
Also known as Static Binding. This is achieved through Method Overloading—where multiple methods have the same name but different parameters. The compiler knows which method to call at the time of compilation.
Also known as Dynamic Method Dispatch or Dynamic Binding. This is achieved through Method Overriding. The call to an overridden method is resolved at runtime based on the actual object type.
This is the most powerful aspect of polymorphism. It allows you to write code that can handle objects of different classes as long as they share a common parent. This is achieved through Upcasting.
Parent obj = new Child();
In this scenario, if both classes have a method named show(), the JVM will execute the version in the Child class because that is the actual object type created in memory.
A recent and more advanced feature of Java polymorphism is the Covariant Return Type. It allows an overriding method to have a return type that is a subclass of the return type declared in the original method of the parent class.
This adds extra flexibility, as the caller gets a more specific object type without needing explicit casting.
Not everything in Java can be polymorphic. To ensure your system's integrity, you must remember these three rules:
In this real-world example, we use a single process() method to handle different types of payments dynamically.
CryptoPayment) without changing the existing code that handles the Payment reference.if-else or switch statements to check the object type, polymorphism handles it automatically.While closely related, they serve different purposes in software design:
| Feature | Inheritance | Polymorphism |
|---|---|---|
| Core Idea | Creating a new class from an old one. | Performing a single action in different ways. |
| Focus | Structure and Reusability. | Behavior and Flexibility. |
| Requirement | Needs the extends keyword. |
Needs Method Overriding/Overloading. |
Q: What is "Object Downcasting" and is it safe?
A: Downcasting is converting a parent reference back to a child object (e.g., Child c = (Child)parent;). It is risky and can throw a ClassCastException. Always use the instanceof operator before downcasting.
Q: Can we achieve polymorphism through data members (variables)?
A: No. Variables in Java do not exhibit polymorphism. If a parent and child have a variable with the same name, the reference type (the parent) decides which variable is accessed, not the object type.
Q: What is the 'instanceof' operator?
A: It is a comparison operator used to test whether an object is an instance of a specific class or interface. It returns true or false.
Polymorphism is the secret ingredient that makes Java applications scalable. It allows you to write "Generic" code that behaves specifically depending on the context. By decoupling the "What to do" (the method call) from the "How to do it" (the implementation), you build software that is ready for the future.
Next: Master Java Abstraction →