Understanding Polymorphism in Java: Concepts, Late Binding, Constructors, and Downcasting
This article explains Java polymorphism—its definition, required conditions, and practical examples—including late binding, constructor behavior, and downcasting—illustrated with clear code snippets and analogies to help readers grasp how a single method call can produce different runtime results.
Polymorphism is the ability of a single behavior to have multiple forms, allowing the same method call to produce different results depending on the object's runtime type.
01 What is Polymorphism
Using an analogy from "Journey to the West," the article describes polymorphism as a capability where a single reference can invoke different implementations, similar to Sun Wukong’s 72 transformations.
In Java, polymorphism means that executing code can yield different outcomes based on the actual object type at runtime.
The three prerequisites for polymorphism are:
Subclass inherits from a superclass.
Subclass overrides a method of the superclass.
Superclass reference points to a subclass object.
A simple demonstration (Program 1‑1) shows a subclass overriding a method and a superclass reference invoking it.
//子类继承父类
public class Wangxiaoer extends Wanger {
public void write() {
//子类覆盖父类方法
System.out.println("记住仇恨,表明我们要奋发图强的心智");
}
public static void main(String[] args) {
//父类引用指向子类对象
Wanger[] wangers = { new Wanger(), new Wangxiaoer() };
for (Wanger wanger : wangers) {
//对象是王二的时候输出:勿忘国耻
//对象是王小二的时候输出:记住仇恨,表明我们要奋发图强的心智
wanger.write();
}
}
}
class Wanger {
public void write() {
System.out.println("勿忘国耻");
}
}02 Polymorphism and Late Binding
The article explains that at compile time the compiler only sees a reference of type wanger , but at runtime it binds to the actual object's method (either the superclass or subclass) based on the object's type.
Late binding enables extensibility, as shown in Program 2‑1 where new methods are added to classes without affecting existing polymorphic calls.
//子类继承父类
public class Wangxiaoer extends Wanger {
public void write() {
//子类覆盖父类方法
System.out.println("记住仇恨,表明我们要奋发图强的心智");
}
public void eat() {
System.out.println("我不喜欢读书,我就喜欢吃");
}
public static void main(String[] args) {
//父类引用指向子类对象
Wanger[] wangers = { new Wanger(), new Wangxiaoer() };
for (Wanger wanger : wangers) {
wanger.write();
}
}
}
class Wanger {
public void write() {
System.out.println("勿忘国耻");
}
public void read() {
System.out.println("每周读一本好书");
}
}The added read() and eat() methods do not interfere with the existing write() polymorphic behavior.
03 Polymorphism and Constructors
When a subclass constructor is invoked, the superclass constructor runs first; if the superclass constructor calls an overridden method, the subclass’s version executes before its fields are initialized, leading to unexpected output.
public class Wangxiaosan extends Wangsan {
private int age = 3;
public Wangxiaosan(int age) {
this.age = age;
System.out.println("王小三的年龄:" + this.age);
}
public void write() {
//子类覆盖父类方法
System.out.println("我小三上幼儿园的年龄是:" + this.age);
}
public static void main(String[] args) {
new Wangxiaosan(4);
// 上幼儿园之前
// 我小三上幼儿园的年龄是:0
// 上幼儿园之后
// 王小三的年龄:4
}
}
class Wangsan {
Wangsan() {
System.out.println("上幼儿园之前");
write();
System.out.println("上幼儿园之后");
}
public void write() {
System.out.println("老子上幼儿园的年龄是3岁半");
}
}The output shows that the superclass constructor calls the overridden write() before the subclass field age is set, resulting in a default value of 0.
04 Polymorphism and Downcasting
Downcasting converts a superclass reference to a subclass type. It is unsafe if the actual object is not an instance of the subclass, leading to ClassCastException . The article demonstrates both successful and failing downcasts.
public class Wangxiaosi extends Wangsi {
public void write() {
System.out.println("记住仇恨,表明我们要奋发图强的心智");
}
public void eat() {
System.out.println("我不喜欢读书,我就喜欢吃");
}
public static void main(String[] args) {
Wangsi[] wangsis = { new Wangsi(), new Wangxiaosi() };
// wangsis[1] can be downcast
((Wangxiaosi) wangsis[1]).write();
// wangsis[0] cannot be downcast and will throw ClassCastException
((Wangxiaosi) wangsis[0]).write();
}
}
class Wangsi {
public void write() {
System.out.println("勿忘国耻");
}
public void read() {
System.out.println("每周读一本好书");
}
}05 Summary
The article emphasizes that polymorphism is one of Java’s three core features, allowing developers to separate changing behavior from stable code, enhance extensibility, and write more flexible programs without modifying existing source.
In the author's words: "Polymorphism means that the concrete type of a reference and the method implementation it invokes are determined at runtime, enabling code reuse and dynamic behavior without altering the original source."
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.