Calling overloaded inherited methods using super class reference

后端 未结 10 1241
既然无缘
既然无缘 2020-12-29 07:38

I do not understand this Java behavior. I have two classes:

class C1 {
    public void m1(double num) {
        System.out.println(\"Inside C1.m1(): \" + num         


        
10条回答
  •  孤城傲影
    2020-12-29 08:24

    Method signatures for both methods are different.

    public void m1(double num) 
    public void m1(int num)
    

    So there is no overriding in this case. Now when you say

        C1 c = new C2();
        c.m1(10);
    

    at compile time, it will se reference is of type C1 which has method public void m1(double num) which is compatible with 10 [int in expanded to double]. So int is promoted to double, and the corresponding method is called (which is also what you see in the bytecodes).

提交回复
热议问题