Calling overloaded inherited methods using super class reference

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

    If you call c.m1(10.0) it will call the method of the ancestor as you were expecting at first.

    You're doing method overloading in your example (that is adding more methods with the same name and different signature) instead of method overriding (that is changing the implementation of an ancestor's method at a descendent by redeclaring it with the same signature, AKA same name and same type of result and of method arguments - argument names shouldn't matter).

提交回复
热议问题