Operator overloading and overriding in Java

后端 未结 5 1421
醉酒成梦
醉酒成梦 2020-11-29 13:25

What is the difference between operator overloading and operator overriding?

Are they the same in inheritance and console program?

5条回答
  •  醉梦人生
    2020-11-29 13:47

    You can overload operator in C++ but not in Java. I wonder if you meant method overloading and method overriding? Method overloading is having two definitions for the same method signature. For example,

    int sum(int var1, int var2)
    {
      return (var1+var2);
    }
    
    int sum(int var1, int var2, int var3)
    {
      return (var1+var2+var3);
    }
    

    In object oriented programming, you override (redefine) a function that is inherited from ascendant (base) class. In a class hierarchy, when a function (method) in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.

提交回复
热议问题