Object-oriented overriding

 What is method override (Override / Overwrite), overload (Overload)? What conditions must be met for method override and overload? 

Override


    1, must have inheritance!
    2, the method name is the same
    3, the parameter list (parameter type, the number of parameters are the same)
    4, the return value type is the
    same access rights

Overriding is the rewriting of the implementation process of the method that is allowed to be accessed by the subclass, and neither the return value nor the formal parameters can be changed. That is, the shell remains unchanged, and the core is rewritten!

The advantage of overriding is that subclasses can define their own specific behavior as needed. This means that subclasses can implement the methods of the superclass as needed.

 

 

Overload

Overloading (overloading) is inside a class, the method name is the same, but the parameters are different. The return types can be the same or different.

Each overloaded method (or constructor) must have a unique parameter type list.

The most common place is the overloading of constructors.

Overloading rules:

  • The overloaded method must change the parameter list (the number or types of parameters are different);
  • Overloaded methods can change the return type;
  • Overridden methods can change access modifiers;
  • Overridden methods can declare new or broader checked exceptions;
  • Methods can be overloaded in the same class or in a subclass.
  • The return value type cannot be used as a distinguishing criterion for overloaded functions.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325259203&siteId=291194637
Recommended