Type order with overloaded methods in Java
java, types
Solution
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.
Problem
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?