Detailed explanation of Java rewriting and overloading methods and differences

Override

Rewriting is a subclass's rewriting of the implementation process of the accessible methods of the parent class! Neither return value nor formal parameter can be changed. That is, the shell remains unchanged and the core is rewritten!

The advantage of rewriting is that subclasses can define their own specific behavior as needed.

That is, the subclass can implement the methods of the parent class as needed.

In the object-oriented principle, rewriting means that any existing method can be rewritten. Examples are as follows:

The compilation and operation results of the above examples are as follows:

In the above example, we can see that although B is of type animal, it runs the move method of dog class.

This is because at compile time, only the reference type of the parameter is checked.

However, at run time, the Java virtual machine (JVM) specifies the type of object and runs the method of the object.

Therefore, in the above example, the reason why the compilation is successful is that there is a move method in the animal class. However, at runtime, the method of a specific object is running. Consider the following examples:

The compilation and operation results of the above examples are as follows:

The program will throw a compilation error because B's reference type animal has no bark method.

Override rules for methods

The parameter list must be exactly the same as that of the overridden method;

The return type must be exactly the same as that of the overridden method;

The access permission cannot be higher than the access permission of the overridden method in the parent class. For example, if a method of the parent class is declared public, overriding the method in the child class cannot be declared protected.

Member methods of a parent class can only be overridden by its subclasses.

Methods declared final cannot be overridden.

Methods declared as static cannot be overridden, but can be declared again.

If the subclass and the parent class are in the same package, the subclass can override all methods of the parent class, except those declared as private and final.

If the subclass and the parent class are not in the same package, the subclass can only override the non final methods declared as public and protected by the parent class.

The overridden method can throw any non mandatory exception, whether or not the overridden method throws an exception. However, the overridden method cannot throw a new mandatory exception, or a wider range of mandatory exceptions than those declared by the overridden method, and vice versa.

Constructor cannot be overridden.

If you cannot inherit a method, you cannot override it.

Use of super keyword

When you need to call a rewrite method of a parent class in a subclass, use the super keyword.

The compilation and operation results of the above examples are as follows:

Overload

Overloading is in a class. The method names are the same, but the parameters are different. What about the return type? It can be the same or different.

Each overloaded method (or constructor) must have a unique list of parameter types.

Only constructors can be overloaded

Overload rule

The overloaded method must change the parameter list;

Overloaded methods can change the return type;

The overloaded method can change the access modifier;

Overloaded methods can declare new or broader check exceptions;

Methods can be overloaded in the same class or in a subclass.

example

The difference between overriding and overloading

I hope this article is helpful to all friends

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
分享
二维码
< <上一篇
下一篇>>