


Does Java Method Overloading Consider Runtime Parameter Types or Only Compile-Time Declared Types?
Overloading Method Selection: Real vs. Declared Parameter Types
In Java programming, overloaded methods provide the flexibility to define multiple methods with the same name but different parameter lists. When a method is invoked, the compiler determines the appropriate method to call based on the argument list provided.
However, in certain scenarios, you may wonder if method selection considers the actual (runtime) type of the parameter instead of the declared type. Consider the following code:
interface Callee { void foo(Object o); void foo(String s); void foo(Integer i); } class CalleeImpl implements Callee { @Override public void foo(Object o) { System.out.println("foo(Object o)"); } @Override public void foo(String s) { System.out.println("foo(\"" + s + "\")"); } @Override public void foo(Integer i) { System.out.println("foo(" + i + ")"); } } public class Main { public static void main(String[] args) { Callee callee = new CalleeImpl(); Object i = new Integer(12); Object s = "foobar"; Object o = new Object(); callee.foo(i); callee.foo(s); callee.foo(o); } }
Unexpectedly, this code prints "foo(Object o)" for all three calls. Contrary to intuition, the method selection does not take into account the actual parameter types (Integer, String, and Object). Instead, it relies solely on the declared parameter types in the Callee interface.
This behavior stems from the Java language specification, which explicitly states:
When a method is invoked, the number of actual arguments and the compile-time types of the arguments are used, at compile time, to determine the signature of the method that will be invoked.
Therefore, it is crucial to note that in Java, dynamic method dispatch, where the actual method to be called is determined at runtime based on the object it is called on, only applies to the object itself, not to the parameter types.
The above is the detailed content of Does Java Method Overloading Consider Runtime Parameter Types or Only Compile-Time Declared Types?. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
