Back to Study Material Java

Polymorphism in Java

Polymorphism is the ability of a single reference variable to refer to objects of different types, with the correct behavior selected automatically dependi...

Polymorphism in Java

One Interface, Many Forms

Polymorphism is the ability of a single reference variable to refer to objects of different types, with the correct behavior selected automatically depending on the actual object. In Java, polymorphism is most commonly observed when a parent-type variable holds a child-type object and methods are called on it. The JVM examines the actual runtime type of the object—not the declared type of the variable—to decide which method implementation to execute.

Compile-Time vs Runtime Polymorphism Compile-time polymorphism (static polymorphism) is achieved through method overloading. The compiler selects the correct overload based on the argument types at compile time. Runtime polymorphism (dynamic polymorphism) is achieved through method overriding combined with inheritance or interfaces. The JVM selects the correct implementation at runtime based on the actual object type. Understanding which form applies in a given situation is essential for predicting program behavior.

Polymorphism Through Interfaces Interfaces amplify polymorphism because a single interface reference can point to objects of completely unrelated class hierarchies, as long as both classes implement the interface. A Printable reference can hold a Document or an Image if both implement Printable. Code written against the Printable interface works with any conforming class added in the future, without any modification. This is the Open/Closed Principle in action: open for extension, closed for modification.

Practical Benefits in Design Polymorphism enables you to write general-purpose code that works with entire families of types. A sort algorithm written against the Comparable interface works with Integer, String, Date, or any custom class that implements compareTo(). A logging framework written against a Logger interface can be backed by a console logger in development and a file logger in production. Swapping implementations requires no changes to the code that uses the interface.

Code Example: interface Drawable { void draw(); }

class Circle implements Drawable { public void draw() { System.out.println("Drawing circle"); } } class Rectangle implements Drawable { public void draw() { System.out.println("Drawing rectangle"); } }

Drawable[] shapes = { new Circle(), new Rectangle() }; for (Drawable d : shapes) { d.draw(); // correct version called for each }

Interview Tip: A classic interview question asks you to predict output when a parent variable holds a child object. Remember: instance method calls use the runtime type (child's override is called), but field accesses use the compile-time type (parent's field is accessed). This asymmetry means fields are never polymorphic, only methods are, and questions about field access through parent references reveal whether you truly understand the mechanism.