Calling overloaded inherited methods using super class reference

后端 未结 10 1284
既然无缘
既然无缘 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:22

    Since C1.m1(double num) is a public method, it inherited C2. So your C2 also has a method, m1(double num), and that's why it is called. From main() you actually called C2.m1(double num).

    Note: Now at class C2 you have two overloaded methods - m1(int num) and m1(double num). And C2.m1(int num) is a different method from C2.m1(double num).

提交回复
热议问题