Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type order with overloaded methods in Java

Tags:

java

types

Given two methods on the same class in Java :

public void doSomething( Person person );
public void doSomething( Employee employee );

where

Employee extends Person

If I call:

doSomething( employee )

I find that doSomething( Person ) gets invoked.

I'd have expected the overload with the closest matching contract be invoked, not with the most abstract (which is what I'm finding)

Could someone explain why?

like image 231
Marty Pitt Avatar asked Jul 02 '09 16:07

Marty Pitt


2 Answers

The most specific applicable overload is used - but that overload is determined at compile-time, based on the compile time type of the employee variable.

In other words:

Employee employee = new Employee();
doSomething(employee); // Calls doSomething(Employee)

but:

Person employee = new Employee();
doSomething(employee); // Calls doSomething(Person)

Note that this is unlike overriding where it's the execution time type of the target object which is important.

like image 62
Jon Skeet Avatar answered Nov 04 '22 12:11

Jon Skeet


How was employee declared? If it was declared as Person employee = new Employee();, then doSomething(Person) is indeed the closest matching overload.

Unlike overrides, overloads are determined at compile time. Therefore, even though the runtime type of employee is Employee, the Person overload has already been chosen to be executed.

See the JLS §8.4.9 Overloading:

When a method is invoked (§15.12), the number of actual arguments (and any explicit type 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 (§15.12.2). If the method that is to be invoked is an instance method, the actual method to be invoked will be determined at run time, using dynamic method lookup (§15.12.4).

like image 20
Michael Myers Avatar answered Nov 04 '22 13:11

Michael Myers