The difference between Java rewriting and overloading

Override

Rewriting is a subclass's rewriting of the implementation process of the accessible methods of the parent class, and the return value and formal parameters cannot 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.

A rewritten method cannot throw a new check exception or an exception that is broader than the declaration of the rewritten method. For example, a method of the parent class declares a check exception IOException, but you cannot throw an exception when overriding this method, because exception is the parent class of IOException and can only throw exceptions of subclasses of IOException.

Override rules for methods

Overload

Overloading is in a class. The method names are the same, but the parameters are different. The return type 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

summary

Method overriding and overloading are different manifestations of Java polymorphism. Rewriting is a manifestation of polymorphism between parent and child classes, and overloading is a manifestation of polymorphism in a class.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of 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
分享
二维码
< <上一篇
下一篇>>