Explain the difference between Java overloading and overriding

Many students don't know the difference between overload and override. It is recommended not to memorize conceptual knowledge by rote, but to understand and remember.

First, let's talk about overloading

(1) Method overloading is a means for a class to handle different types of data in a unified way. Multiple functions with the same name exist at the same time and have different parameter numbers / types. Overloading is a manifestation of polymorphism in a class.

(2) Java method overloading means that multiple methods can be created in a class, which have the same name but different parameters and different definitions. When calling a method, it determines which method to use by passing them different parameter numbers and parameter types, which is polymorphism.

(3) When overloading, the method name should be the same, but the parameter types and numbers are different, and the return value types can be the same or different. The return type cannot be used as the distinguishing standard of overloaded functions.

Let's take an example of overloading:

Observe:

(1) There are four methods with the same name in overloadparent

(2) The parameter types and numbers of the first three methods are inconsistent, and the return values are consistent, forming an overload

(3) The return value of method 4 is different from that of method 1. It does not constitute an overload and the compiler does not pass.

PS: the return value is the result after the method is executed. When calling the method, we will not specify that "I want to call a method with the return value of XXX", which cannot become the feature of method overloading.

(4) Overloadparent inherits the demo and owns all the methods in the demo. It feels that the existing methods can not meet the requirements and simply overloads one.

Overloaded flag: the method name is the same, the parameters are different (number or type), and it has nothing to do with the return value.

Then let's talk about overriding

(1) The polymorphism between parent and child classes redefines the functions of the parent class. If a method defined in a child class has the same name and parameters as its parent class, we say that the method is overridden. In Java, subclasses can inherit the methods in the parent class without rewriting the same methods.

However, sometimes subclasses do not want to inherit the methods of the parent class intact, but want to make certain modifications, which requires method rewriting.

Method overrides are also called method overrides.

(2) If a method in the subclass has the same method name, return type and parameter table as a method in the parent class, the new method will overwrite the original method.

If you want the original method in the parent class, you can use the super keyword, which refers to the parent class of the current class.

(3) The access rights of subclass functions cannot be less than those of the parent class;

Let's take another example of overwriting:

What will be output when overridechild's main method is executed?

The answer is: I can't fly, but I can run!

We see:

(1) Both overridechild and overrideparent have a fly method

(2) The return value and modifier of fly are the same, except for the method body

(3) There is a @ overwrite annotation in front of the fly method of the subclass. It appears in JDK1.5 and is only used for class inheritance. 1.6 can be used for interface implementation. This annotation is helpful for compiler inspection and can be used without.

Overridden flag: the child inherits the parent class and has different implementations for the same method.

Application scenario

Overloading: when methods have similar functions, but need to pass different parameters.

Override: a subclass has its own unique behavior. It inherits from the parent class and cannot meet its own needs.

PS: overloading and overwriting are polymorphic expressions. The former is compiler polymorphism and the latter is runtime polymorphism.

Rules for overriding methods:

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

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

3. The access modifier must be more restrictive than the access modifier of the overridden method (public > protected > Default > private)

4. Rewriting methods must not throw new check exceptions or check exceptions that are broader than the declaration of the rewritten method. For example:

A method of the parent class declares a check exception IOException. When overriding this method, you cannot throw an exception. You can only throw subclass exceptions of IOException, and you can throw non check exceptions.

Overloaded rules:

1. Must have different parameter lists;

2. You can have non scolding return types as long as the parameter lists are different;

3. There can be different access modifiers;

4. Different exceptions can be thrown;

Rewriting differs from overloading in that:

Rewriting polymorphism works. Calling overloaded methods can greatly reduce the amount of code input. As long as the same method name passes different parameters, it can have different functions or return values.

Using rewriting and overloading well can design a class with clear and concise structure. It can be said that rewriting and overloading play an extraordinary role in the process of writing code

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>