The difference between overloading and rewriting

Overloading

 Method overloading is a way for classes to handle different types of data in a uniform way. Multiple functions with the same name exist at the same time, with different number/types of parameters.

Overloading Overloading is a manifestation of polymorphism in a class.

 Java 's method overloading is that you can create multiple methods in a class, they have the same name, but have different parameters and different definitions.

When calling methods, it is determined which method to use by the different number of parameters and parameter types passed to them, which is polymorphism.

 When overloading, 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.

When the parent class method is modified by default, it can only be overridden by its subclasses in the same package, and cannot be overridden if it is not in the same package.

When the method of the parent class is protoeted, it is not only overridden by its subclasses in the same package, but also can be overridden by subclasses of different packages.

 Rules for overriding methods :

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

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

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

4) The overridden method must not throw a new checked exception or a checked exception that is wider than that declared by the overridden method. E.g:

A method of the parent class declares a checked exception IOException. When overriding this method, Exception cannot be thrown, only subclass exceptions of IOException can be thrown, and unchecked exceptions can be thrown.

 And the overloaded rules:

1), must have different parameter lists;

2), there can be different return types, as long as the parameter list is different;

3), can have different access modifiers;

4), can throw different exceptions;

Features of overloading and overriding (overriding):

1. Override Features

  1. The flag of the overridden method must exactly match the flag of the overridden method to achieve the effect of coverage;

  2. The return value of the overridden method must be consistent with the return value of the overridden method;

  3. The exception thrown by the overridden method must be the same as the exception thrown by the overridden method, or a subclass thereof;

  4. The overridden method cannot be private, otherwise only a new method is defined in its subclass, and it is not overridden.

2.Overload Features

  1. You can only pass different parameter styles when using overloading. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types in the same method must be different, such as fun(int, float), but not fun(int, int) ));

  2. It cannot be overloaded by access rights, return types, and thrown exceptions;

  3. The exception type and number of methods will not affect overloading;

  4. For inheritance, if a method is priavte in the parent class, it cannot be overloaded in the subclass. If it is defined, it will only define a new method, and will not achieve overloading. loading effect.



Summarize:

override

   1. The method name, parameters and return value are the same.

   2. Subclass methods cannot reduce the access rights of parent class methods.

   3. Subclass methods cannot throw more exceptions than parent class methods (but subclass methods can throw no exceptions).

   4. It exists between the parent class and the child class.

   5. The method is defined as final and cannot be overridden.

 overload

  1. At least one of the parameter type, number, and order is different. 

  2. You cannot overload method names that only have different return values.

  3. Exist in the parent class and subclass, the same kind.

Guess you like

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