Method rewriting and overloading

Method rewriting and overloading

Method rewriting (overwriting)

Requirement: The name of the method is consistent
. The rewriting (overwriting) of the method must occur between the parent and child

  • Execution rules: New who runs whose method has nothing to do with the declared type, and is determined by the type of the object created
  • Method coverage definition requires consistent method names
  • The method range in the subclass must be greater than or equal to the method range in the parent class. Private return types are not allowed to be consistent. The subclass methods can be less than or equal to the parent class type, such as the parent class Number and the subclass Integer parameter types.
  • The parameters of the method are consistent (number, type, order), regardless of the parameter name
  • The return data type is consistent [Interview] (Because if the return type is inconsistent, the grammar check cannot be performed. For example, the parent class returns Double, and the child class returns Integer. Is the grammar check at the calling site checked by Double or by Integer? The parent type is returned, and the subtype is returned in the subclass. For example, the Number type is returned in the parent class, while the Integer is returned in the subclass.)

note! ! ! !

Static method coverage and calling:: Whoever declares it will call whose static method

Method overload

The method names are the same, and the parameters are different, regardless of the return value type.
Rules can be invoked within a class or between parent and child classes : the matching principle of good type has the
same method name and different parameters. There are three cases of different parameters: the number of parameters is different, the parameter types are different, the parameter order is different, and the parameter name is irrelevant

1. It has nothing to do with the return type
2. It has nothing to do with the scope
3. It has nothing to do with the exception thrown by the method

Guess you like

Origin blog.csdn.net/qq_45874107/article/details/112736643