In Java, polymorphism is divided into two types: compile-time polymorphism (overloading) and run-time polymorphism (rewriting). Compile-time polymorphism is also called front binding, and run-time polymorphism is also called post-binding.
The following is an example:
public class OverloadAndOverwrite { public static void main(String[] args) { A a1 = new A(); A a2 = new B(); B b = new B(); C c = new C(); D d = new D(); System.out.print("a1.print(a1): "); a1.print(a1);//输出A and A System.out.print("a1.print(b): "); a1.print(b);//输出A and A:原因是因为A中不存在参数为B的方法,因此会调用参数为A的方法,因为B是继承自A的 System.out.print("a1.print(c): "); a1.print(c);//输出A and A:原因是因为A中不存在参数为C的方法,因此会调用参数为A的方法,因为C是继承自B的,B是继承自A的 System.out.print("a1.print(d): "); a1.print(d);//输出A and D:原因是因为A中存在参数为D的方法,因此会调用参数为D的方法 System.out.print("a2.print(b): "); a2.print(b);//输出B and A:原因在于首先入口是A,首先查看A中是否有参数为B的print方法,发现没有那就寻找有没有参数为A的方法,因为B是继承自A的,发现存在这样的方法,那么再次查看B中有没有重写这个方法,发现有重新,直接调用B中这个重写的方法 System.out.print("a2.print(c): "); a2.print(c);//输出B and A:原因在于首先入口是A,首先查看A中是否有参数为C的print方法,发现没有那就寻找有没有参数为B的方法,因为C是继承自B的,发现也不存在这样的方法,那就寻找存在参数为A的print方法,因为B继承自A,发现存在这样的方法,那么再次查看B中有没有重写这个方法,发现有重新,直接调用B中这个重写的方法 System.out.print("a2.print(d): "); a2.print(d);//输出 A and D:原因在于入口是A,查看A中存在参数为D的方法,再次查看B中没有重写这个方法,因此输出A中这个方法的结果即可; System.out.print("a2.print(a2): "); a2.print(a2);//输出B and A;原因在于a2的类型是A,因此会调用A里面参数为A的print方法,但是a2右边new的是B,所以因为B中有参数为A的方法,因此采用的是B里面的这个方法 System.out.print("b.print(b): "); b.print(b);//输出B and B;原因:入口是B,因此查看B中存不存在参数为B的print函数,存在则直接输出; System.out.print("b.print(c): "); b.print(c);//输出B and B;原因:入口是B,因此查看B中存不存在参数为C的print函数,发现不存在,则查看存不存在参数为B的print函数,发现存在,并且C中并没有重写该方法,则直接输出;有一点需要注意的是还需要查看一下A中是否存在参数为C的print方法,因为B继承自A,有的话会及成果来这个方法,这样的话输出的结果将变为A and C System.out.print("b.print(d): "); b.print(d);//输出A and D;原因:入口是B,虽然B中不存在参数为D的print函数,但是B继承自A,A中是存在参数为D的print函数的,因此输出的是A中参数为D的结果; } } class A { public void print(A a) { System.out.println("A and A"); } public void print(D d) { System.out.println("A and D"); } // public void print(C c) // { // System.out.println("A and C"); // } } class B extends A { public void print(B b) { System.out.println("B and B"); } public void print(A a) { System.out.println("B and A"); } } class C extends B{} class D extends C{}
What needs to be explained here is:
For A a2 = new B();
If printed separately If a2 is output, the printed result is B@ (hash code), not A@ (hash code), but this does not mean that the type of a2 is B type, because when we call a2.print(a2) in the above program ; when the output result is B and A instead of A and A (if you assume that a2 is of type B, you should call the print method with parameter B in class A, because there is no such method, then the second best option is to call The method with parameter A should output A and A, because B is a subclass of A).
The above is an example code analysis of rewriting and overloading. I hope it will be helpful to students learning Java.
For more articles related to Java function overloading and rewriting example codes, please pay attention to the PHP Chinese website!

This article analyzes the top four JavaScript frameworks (React, Angular, Vue, Svelte) in 2025, comparing their performance, scalability, and future prospects. While all remain dominant due to strong communities and ecosystems, their relative popul

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

Node.js 20 significantly enhances performance via V8 engine improvements, notably faster garbage collection and I/O. New features include better WebAssembly support and refined debugging tools, boosting developer productivity and application speed.

This article addresses the CVE-2022-1471 vulnerability in SnakeYAML, a critical flaw allowing remote code execution. It details how upgrading Spring Boot applications to SnakeYAML 1.33 or later mitigates this risk, emphasizing that dependency updat

Iceberg, an open table format for large analytical datasets, improves data lake performance and scalability. It addresses limitations of Parquet/ORC through internal metadata management, enabling efficient schema evolution, time travel, concurrent w

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
