Using inherited overloaded methods

后端 未结 11 1763
北荒
北荒 2020-12-15 05:50

I have two classes:

public class ClassA {
    public void method(Number n) {
        System.out.println(\"ClassA: \" + n + \" \" + n.getClass());
    }
}
         


        
11条回答
  •  天命终不由人
    2020-12-15 06:13

    In overloading, method resolution always takes care by compiler based on reference type. So, In overloading runtime object[new ClassB()] does not play any role. Therefore, in your case the method of ClassA was executed.

    ClassA a = new ClassB(); 
    a.method(3);
    

提交回复
热议问题