Java Puzzle – Method Overloading Part 2

This is with reference with previous block, but next level.

Program

public class OverloadingPuzzle {
public void display(Object obj){
System.out.println("Object version");
}

public void display(String str){
System.out.println("String Version");
}
public void display(Integer str){
System.out.println("Integer Version");
}

public static void main(String[] args){
OverloadingPuzzle obj = new OverloadingPuzzle();
obj.display(null);
}
}

Now I am confused, will it print String Version or Integer Version.

Output

like me Compiler is also confused here, there will be compilation error

Uncompilable source code - reference to display is ambiguous, both method display(java.lang.String) in testproject.OverloadingPuzzle and method display(java.lang.Integer) in testproject.OverloadingPuzzle match

Comments

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester