Fundamentals 6 min read

Understanding Polymorphism in Java

This article explains Java polymorphism, covering its definition, dynamic binding, necessary conditions, benefits such as substitutability and extensibility, and demonstrates implementation through interfaces, inheritance, method overriding and overloading, accompanied by sample code and a quiz with expected outputs.

Java Captain
Java Captain
Java Captain
Understanding Polymorphism in Java

Java Polymorphism Implementation

What is polymorphism

Polymorphism is one of the three major object‑oriented features (encapsulation, inheritance, polymorphism). It allows objects of different classes to respond to the same message, meaning the same method call can exhibit different behaviors depending on the actual object type.

Definition : Polymorphism permits different classes to respond to the same message; the same message can result in multiple behaviors (a method call).

Technique : Dynamic binding (runtime determination of the object's actual type) enables polymorphism.

Purpose : It reduces coupling between types.

Real‑world example : Pressing F1 opens the help document appropriate to the current application (Flash, Word, Windows), illustrating the same event producing different results.

Three necessary conditions for polymorphism:

1. Inheritance 2. Method overriding 3. A superclass reference pointing to a subclass object.

Benefits of polymorphism

1. Substitutability – code written for a superclass works with any subclass (e.g., a method that works with Circle also works with Ring).

2. Extensibility – adding new subclasses does not affect existing code.

3. Interface‑ability – the superclass defines a common interface (e.g., computeArea(), computeVolume()) that subclasses implement or override.

4. Flexibility – enables diverse operations and improves efficiency.

5. Simplicity – simplifies writing and modifying code, especially when handling many objects.

Implementation in Java can be achieved via interface implementation, inheritance with method overriding, or method overloading within the same class.

Sample classes

class A {
    public String show(D obj) { return "A and D"; }
    public String show(A obj) { return "A and A"; }
}
class B extends A {
    public String show(B obj) { return "B and B"; }
    public String show(A obj) { return "B and A"; }
}
class C extends B { }
class D extends B { }

Quiz

A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println(a1.show(b)); // ①
System.out.println(a1.show(c)); // ②
System.out.println(a1.show(d)); // ③
System.out.println(a2.show(b)); // ④
System.out.println(a2.show(c)); // ⑤
System.out.println(a2.show(d)); // ⑥
System.out.println(b.show(b));  // ⑦
System.out.println(b.show(c));  // ⑧
System.out.println(b.show(d));  // ⑨

Answers

① A and A
② A and A
③ A and D
④ B and A
⑤ B and A
⑥ A and D
⑦ B and B
⑧ B and B
⑨ A and D

Original source: http://www.cnblogs.com/jack204/archive/2012/10/29/2745150.html

If you encounter problems while learning Java or want resources, you are welcome to join the Java study group QQ: 495273252.

JavaooppolymorphismInheritancemethod overloadingDynamic Binding
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.