1

Java uses early binding to determine which method to call, but here my question is how is it determining that it should call

private static void confuse(RuntimeException object)

not the other two methods ?

/**
 * @param args
 */
public static final void main(String[] args) {
    confuse(null);
}

private static void confuse(Object object) {
    System.out.println("object");

}

private static void confuse(RuntimeException object) {
    System.out.println("RuntimeException ");

}
private static void confuse(Exception object) {
    System.out.println("Exception ");

}

if I am calling confuse using null it should give compilation error, as null can be the value for all three classes. Please explain the output.

ankit
  • 4,919
  • 7
  • 38
  • 63
  • I have read the answers on that question and still that does not answer how is it calling private static void confuse(RuntimeException object) not the other two – ankit Oct 05 '14 at 18:30
  • 1
    Maybe this answer will help you http://stackoverflow.com/a/13033089/116249 – Patrick Oct 05 '14 at 18:32
  • 1
    I was a little eager on that close. Basically, `RuntimeException` is a more specific type than the others, so it is chosen. There are other duplicates around. – Sotirios Delimanolis Oct 05 '14 at 18:32
  • @SotiriosDelimanolis Now I got my answer so we can close this :) – ankit Oct 05 '14 at 18:34
  • @Patrick thanks for the link this answers my question – ankit Oct 05 '14 at 18:35
  • @Sotirios Delimanolis it has an exact duplicate see above this comment. It explains why RuntimeException will be called this time. – Alboz Oct 05 '14 at 18:39

0 Answers0