difference between overriding and overloading

Overload description:

  In the same class, the method name must be the same, but the parameter type and number are different, and the return value type can be the same or different. The return type cannot be used to distinguish overloaded functions. Subclasses can overload the methods of the parent class, but when the method of the parent class is modified by the default modifier, it can only be overloaded under the same package. It can only be determined according to different parameter styles. If the parent class is overloaded The modifier is private, you cannot define overloading in subclasses, but only add a new method.

  Rules Description:

           must have different parameter lists;

          can have different return types;

          can have different access modifiers;

          Can throw different exceptions; 

Rewrite instructions:

  Overriding only happens in inheritance, and it is the method of the subclass that overrides the superclass  

       Rules Description:

      The parameter list must be exactly the same as the method being overridden, otherwise it cannot be called an overriding but an overloading.

      The return type must always be the same as the return type of the overridden method, otherwise it cannot be called overriding but overloading.

      The restriction of the access modifier must be greater than the access modifier of the overridden method (public>protected>default>private)

      Overriding methods must not throw new checked exceptions or checked exceptions that are more general than those declared by the overridden method, and can only throw exceptions of the parent class, or subclasses of the parent class exception

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326645655&siteId=291194637