The difference between method overriding and method overloading

1. Method overriding (0veriding)

In a Java program, the inheritance relationship of a class can generate a subclass, and the subclass inherits the parent class. It has all the characteristics of the parent class and inherits all the methods and variables of the parent class.

Subclasses can define new features. When a subclass needs to modify some methods of the parent class to expand and increase functions, programmers often call such an operation method overriding, also known as overriding or overwriting.

Rewriting embodies the superiority of Java. Rewriting is based on the inheritance relationship, which enriches the language structure. In inheritance in Java, a subclass can both hide and access the methods of the parent class and override the methods of the inherited parent class.

Overriding the method of inheriting the parent class in Java is achieved by overriding the method. The so-called method overriding means that the method in the subclass has exactly the same return value type, method name, number of parameters and parameter types as the inherited method in the superclass.

In this way, you can achieve the override of the parent class method. If the subclass overrides the method in the parent class, the overridden method must be called when it is called, so what if the method in the parent class must be called now?

At this point, this function can be achieved by using the super key. The super keyword can access the content of the parent class from the subclass. If you want to access the overridden method, use the form of "super.method name (parameter list)" transfer.

If you want to use the super keyword, you don't have to use it after the method is overridden, you can also explicitly indicate that a method is inherited from the parent class. Using super just makes it more explicit that if you want to look up from the parent class, you don't look in the child class.

 

2. Rewriting rules
When rewriting methods, the following rules need to be followed: 
(1) The parameter list of the parent class method must be exactly the same as the parameter list of the method overridden by the subclass, otherwise it cannot be called rewriting but rewriting load.
(2) The return type of the parent class must be the same as the return type of the method overridden by the subclass, otherwise it cannot be called overriding but overloading. .. 
(3) It is stipulated in Java that the method overridden by the subclass cannot have more strict access rights than the method of the superclass. Anyone who has written Java programs knows that,

The method in the parent class cannot be overridden under any circumstances. When the access modifier of the method in the parent class is private, the method can only be accessed by its own class.

Cannot be accessed by external classes and cannot be overridden in subclasses. If the method of defining the parent class is public and the subclass is defined as private, an error will be reported when the program runs.

(4) Because the limit of the access modifier of the parent class must be greater than the access modifier of the method overridden by the subclass, and the private permission is the smallest.

So if a method has private access in the parent class, it cannot be overridden in the subclass. If it is redefined, it will only define a new method, and will not achieve the effect of rewriting.
(5) If the method in the parent class throws an exception during the inheritance process, then when the method of the parent class is rewritten in the child class, an exception should also be thrown,

And the exception thrown cannot be more than the exception thrown in the parent class (can be equal to the exception thrown in the parent class). In other words, the overridden method must not throw a new checked exception,

Or a checked exception that is more general than the overridden method declaration. For example, a method of the parent class declares a checked exception IOException, and cannot throw Exception when overriding this method.

Only subclasses of IOException can be thrown, and unchecked exceptions can be thrown. In the same way, if a member variable is created in the subclass,

This variable has the same name as a variable in the parent class, which is called variable override or property override. However, this concept is generally rarely studied because it is of little significance.

 

Three, method overloading (Overloading)

Method overloading is a way for classes to handle different types of data in a uniform way. When a method is called, it is determined which method to use by passing different numbers and types of parameters to them, which is called polymorphism.
The so-called method overloading means that in a class, multiple methods have the same method name but different parameter lists. A different parameter list refers to a different number of parameters, parameter types, or order of parameters.

Method overloading is often used in practical applications. Not only general methods, but also constructors can be overloaded.

When the method is overloaded, there needs to be a certain connection between the methods, because this can improve the readability of the program, and generally only methods with similar functions are overloaded.

Overloading means that we can define some methods with the same name, distinguish these methods by defining different parameters, and then when calling, the Java virtual machine will select the appropriate method to execute according to different parameter lists

. That is, when an overloaded method is called, Java uses the type or number of parameters to determine which overloaded method is actually called. Therefore, the type or number of parameters of each overloaded method must be different.

Although each overloaded method can have a different return type, the return type is not enough to distinguish which method is used.

When Java calls an overloaded method, the method whose parameters match the calling parameters is executed. When using overloading, pay attention to the following points:
1. When using overloading, you can only pass different parameter lists, and must have different parameter lists.

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. There can be different return types, as long as the parameter list is different.
5. There can be different access modifiers.

6. Different exceptions can be thrown.

 

Fourth, the difference between method overriding and method overloading

Guess you like

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