Interview Assault: What is the difference between method rewriting and method overload?

Method rewriting (OVERRIDE) and method overload are object-oriented, and the polymorphism is different, but the two itself is not related, and their differences are like Madews in Liu Dehua’s difference, except for the name Outside, others are unlike.

Let’s take a specific difference between the two.

Over-write

Method Override is a language characteristic, which is a specific manifestation that allows subclasses to redefine existing methods in the parent class, and the method names and parameter types and numbers in the subclass must be with the father. The class is consistent, which is to rewrite.

1.1 Basic Usage

The basic usage of the method rewriting is as follows:

/ *** There is a Method method in the parent class * / classfather {publicvoidMethod (StringName) {system.out.println (“Father:” + name);}} / *** Override the parent class Method Method * / ClassSonextendsfather {@OverridepublicvoidMethod (StringName) {// Subclass redefined the printed behavior, no longer father: xxx, but Son: xxxsystem.out.println (“Son:” + name);}

1.2 use scene

Methods There are many scenarios to rewrite applications in Java, which is more classic is the equals method in the Object class. Object is the parent class of all classes, and the implementation source code in the Equals method in the Object class is as follows:

As can be seen from the above source code, Equals in the Object class is used to compare the reference addresses of the two objects. But for the string, it is finated, so each created string reference address is different, then the comparison reference address is meaningless, so the String class must rewrite the Equals method in the parent class Object. The specific source code is as follows:

Through the above code, we can see that the equals method in the string is no longer comparing whether the reference address of the two strings is the same, but it is determined whether the value of the two strings is the same.

1.3 Precautions

There are five items that need to be noted when the method is rewritten:

Note 1: The permissory control of the subclass method cannot be smaller, that is, if the privilege control of the parent class is protected, the method rights control of the subclass can only be protected or public; precautions 2: Subclass method The type returned can only be smaller, that is, if the parent class method returns a Number type, then the subclass method can only return the subclass of the Number type or Number class, and cannot return the Number type parent class type Object; Precautions 3: Subclass Throwing Exception Types can only be smaller; Precautions 4: Sub-class method names must be consistent with the parent class method name; Precaution 5: Parameter type and number of subclass methods must be in the parent class be consistent.

2. Method Overload

Method Overloading refers to the same class, defines multiple synonymous methods, but the number of parameter types or parameters of the same name method is the method overload.

2.1 Basic use

The following four Method methods are method overload:

publicclassOverloadExample {publicstaticvoidmethod () {// doSomething} publicstaticvoidmethod (Stringname) {// doSomething} publicstaticvoidmethod (Integerid) {// doSomething} publicstaticvoidmethod (Integerid, Stringname) {// doSomething}}

2.2 Using Scenes

Method Overloaded Classic Using Scenario is the valueof method of the String type. There are 9 implementations of the ValueOF method, as shown below: Image It can convert arrays, objects, and basic data types into string types.

2.3 Precautions

The priority matching principle of the method overload has the following 5:

1 Matching Principles: Priority Match the same data type, method overload will prioritize the same method, such as only one String type parameter call, will give priority to only one String parameter type overload method; second match Principle: If it is the basic data type, it will automatically convert to a larger basic data type. For example, the parameter called is the int type, then the basic type Long, not the package type Integer (this requires special attention, easy Error). Section 3 Matching Principles: Automatic Packing and Auto Packed Matching, parameter calls will also perform automatic unpacking and automatic packing methods, such as calling parameters passing the int type, then it can match the INTEGER type overload method ; 4th matching principle: Follow the parent class in turn according to the inheritance route, if the match does not match the current class, try to match its parent class, or the parent class of the parent class, match it up, the 5 match principle: Variable parameter matching, if the method is an optional parameter method, its call priority is the lowest, and the optional parameter method is matched in the final stage. Method Overloads will match the above five principles, and the method of conforming to the rules will be prioritized. In addition to the above matching principles, special attention is required, and different return types cannot be used as the basis for the method, that is, different return value types are not the method to be overloaded, and the detailed description of this is referred to: https: / /MP.weixin.qq.com/s/4pi1ozx8so6gjhd6yxjb3q

3. Method rewriting VS method overload

Methods of rewriting and method overloading are different, mainly reflected in the following aspects:

Difference Method Rewind Method Retrofit method Relationship Description is description of the method of the parent class and subclass It is the method of multiple symbols in the same class. The override keyword needs to use @Override keyword modifications that do not require parameter type and parameter number. You cannot modify the return type. You can only be changed or the same, can not be changed An exception can only be changed or deleted, and the new or more exceptions can be thrown can only be changed, and they cannot be changed.

Summarize

Method rewriting and method overload is object-oriented, different embodiments of polymorphism, and method rewrite describes the method relationship of parent class and subclasses, while the method is descriptive. Method relationship of multiple synonymous methods in a class. In addition to this method rewriting and method overloading: Override keyword, parameter type, and parameter number, return type, throwing exceptions, and permission controls.

This article reprinted from the WeChat public number “Java Interview Zheus”, you can pay attention to the following QR code. Reprint this article Please contact the Java interview question to resolve the public number.