Collection of articles: gitee.com/mydb/interv…

Method Override and method Overload are different manifestations of polymorphisms in object-oriented programming, but they are not related to each other. The difference between them is like the difference between Edward Ma and Andy Lau, except for their names.Let’s take a look at the differences.

1. Method rewrite

Method Override is a language feature. It is a concrete manifestation of polymorphism. It allows a subclass to redefine an existing method in the parent class, and the method name, parameter type and number in the subclass must be the same as the parent class.

1.1 Basic Usage

The basic usage of method overrides is as follows:

/** * the parent class has a method method */
class Father {
    public void method(String name) {
        System.out.println("Father:"+ name); }}/** * subclass overrides method */
class Son extends Father {
    @Override
    public void method(String name) {
        // The subclass redefines the behavior of printing in Father:XXX instead of Son:XXX
        System.out.println("Son:"+ name); }}Copy the code

1.2 Application Scenarios

Method rewriting is used in a number of different scenarios in Java, the classic of which is the Equals method in the Object class. Object is the parent of all classes. Equals is the parent of all classes.The equals class is used to compare whether two objects have the same reference address. In the case of strings, however, it is final, so the reference address is different each time the String is created. In this case, there is no point in comparing the reference addresses. Therefore, String must override the equals method of its parent class Object.We can see that the equals method of a String is no longer comparing whether two strings have the same reference address, but whether two strings have the same value.

1.3 Precautions

Here are five things to be aware of when rewriting a method:

  • Note 1: If the parent method’s permission controller is protected, then the subclass method’s permission controller must be protected or public.
  • Note 2: The type returned by a subclass method must be small. That is, if a subclass method returns Number, then a subclass of Number can only return Number or Long, but not Object.
  • Note 3: Subclasses can only throw exceptions of smaller types;
  • Note 4: The subclass method name must be the same as the parent class method name.
  • Note 5: The parameter types and number of subclass methods must be the same as those of the parent class.

2. Method overload

Method overloading refers to the definition of multiple methods with the same name in the same class, but the parameter types or the number of parameters of the same method are different.

2.1 Basic Usage

The following four methods are method overloads:

public class OverloadExample {
    public static void method(a) {
        // doSomething
    }

    public static void method(String name) {
        // doSomething
    }

    public static void method(Integer id) {
        // doSomething
    }

    public static void method(Integer id, String name) {
        // doSomething}}Copy the code

2.2 Application Scenarios

The classic use scenario for method overloading is the valueOf method of String. ValueOf method overloading has nine implementations, as shown in the following figure:It can convert arrays, objects, and underlying data types to string types.

2.3 Precautions

There are five preferential matching principles for method overloading:

  • The first matching principle is that methods with the same data type will be called in preference to overloaded methods with the same parameter type. For example, overloaded methods with only one String parameter type will be matched in preference to overloaded methods with only one String parameter type.
  • Matching rule 2: If it is a primitive data type, it will be automatically converted to a larger primitive data type for matching. For example, if the call parameter is int, the primitive type long will be called in preference to the wrapper type Integer (this is special and error-prone).
  • 3. Matching principle: Automatic boxing and automatic unboxing match, parameter call will also be automatic unboxing and automatic boxing method match, for example, the call parameter is passed int, then it can match Integer overloaded method;
  • The fourth matching principle: the parent class will be matched up according to the inheritance line. If the current class cannot be matched, the parent class will be matched, or the parent class of the parent class will be matched up.
  • Matching rule 5: variable parameter matching, if the method is an optional parameter method, then it has the lowest call priority, and the optional parameter method will be matched at the last stage.

Method overloads are matched according to the above five rules, and methods that meet the rules are called first. In addition to the above matching principle, but also need special note that different return types cannot serve as the basis of method overloading, namely different return value type is not method overloading, about this details may refer to: mp.weixin.qq.com/s/4pi1OZx8S…

3. Method rewriting vs. method overloading

The specific differences between method rewriting and method overloading are mainly reflected in the following six aspects:

The difference between Methods to rewrite Method overloading
Methods relationship Describes method relationships between parent and child classes Describes method relationships between methods of the same name in the same class
The Override keyword The @override keyword is required Don’t need
Parameter type and number of parameters Do not modify Must be modified
The return type Can only be smaller or the same, not bigger Can be modified
An exception is thrown Can only be reduced or deleted, cannot throw new or larger exceptions Can be modified
Permission control character You can only get bigger, not smaller Can be modified

conclusion

Method Override and method Overload are different manifestations of polymorphism in object-oriented programming. Method Override describes method relations of parent and subclass, while method Overload describes method relations of multiple methods with the same name in the same class. Other differences include the Override keyword, parameter type and number, return type, throw exception, and permission control character.

Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.

Public account: Java Chinese Community