1

Given code:

public static void main(String[] args)
{
    doSomething(new ArrayList());
}

public static void doSomething(Collection collection) {
    System.out.println("Collection here!");
}

public static void doSomething(List list) {
    System.out.println("List here!");
}

This prints out List here! as expected, but is this behaviour defined somewhere in the Java Specifications so I can rely on it, given any Java implementation?

Basti Funck
  • 1,400
  • 17
  • 31
  • 1
    It calls the most specific method (I'm pretty sure this is a duplicate) – JonK Oct 15 '15 at 13:45
  • 1
    Yes, it's defined in the [JLS](https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.2). – Eran Oct 15 '15 at 13:45
  • 1
    Even though the behaviour is well-defined, IMHO you should not rely on it. Either make both overloaded versions do the same thing for the same parameters, or don't use overloading. – Paul Boddington Oct 15 '15 at 13:52
  • The question isn't "How does it behave", it is more of a "Is this defined behaviour or implementation dependent". @PaulBoddington Not intuitive if you work on a framework and have different method names for the same task with different arguments, but propably the safer approach – Basti Funck Oct 15 '15 at 13:58

2 Answers2

3

At compile time most specific method choosen to invoke.

In your case

ArrayList > List > Collection

Since ArrayList is the most specific sub type of List and List is the most Specific subtype of Collection.

Here is specification on rules for method invokation

https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.2

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

There's even more interesting behaviour:

public static void main(String[] args)
{
    Collection myCollection = new ArrayList();
    doSomething(myCollection);
}

public static void doSomething(Collection collection) {
    System.out.println("Collection here!");
}

public static void doSomething(List list) {
    System.out.println("List here!");
}

This will print Collection here!

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • 2
    Does this answer the question? It just seems like an observation about the behavior. – resueman Oct 15 '15 at 13:52
  • Isn't this expected behavior ? – Ghazanfar Oct 15 '15 at 13:56
  • Yes it's expected behaviour. No it doesn't answer the question as others have said that the most specific method is chosen. I thought I'd highlight that it's early bound rather than late bound / polymorphic. – lance-java Oct 15 '15 at 13:59
  • 2
    Doesn't anwsers my question, I know that (atleast) the Oracle Java implementation behaves like this, but I want to know if this is specified, so I can rely on this behaviour independently of the Java Implementation. But thanks for pointing out that additional behaviour. – Basti Funck Oct 15 '15 at 14:00
  • 1
    Even without looking up the JLS you can be quite sure that the language basics will work consistently across different Java implementations. – user140547 Oct 15 '15 at 14:03