[Java] Don't be confused about overloading and rewriting

overload

Method overloading is OverLoading in English, which refers to defining multiple methods with the same name but different parameters in a class . When calling, the corresponding method will be selected according to different parameter expressions. Overloaded methods can modify the return type and return permission , but be careful not to modify only the two. If you only modify the return type or access permission, then call the method Indistinguishable at all. Which method is actually called, in short, as long as the method can be distinguished, there will be no confusion.

rewrite

The English name of method rewriting is Overwrite or Override, which means that the subclass overrides the method logic of the parent class. The overridden method of the subclass must have the same method name, parameter list and return type as the overridden method of the parent class. And the overridden method cannot use stricter access permissions than the overridden method. For example, the overridden method of the parent class is public and the subclass cannot be private.

Summary :

Overloading occurs between the same class, and rewriting occurs between subclasses and superclasses.

Overloading must modify method parameters, rewriting cannot modify method parameters

Guess you like

Origin blog.csdn.net/weixin_43918614/article/details/124072797