Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The difference between overloading and rewriting in java

Shulou Source: shulou.com Published: 2022-06-02 03:57:52 11月03日 Update

This article introduces the knowledge of "the difference between overloading and rewriting in java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Overloading: for example, a method in a class, such as the add () method, can be expressed in three forms, add (int a), or add (int aline int b), or add (int a line int bline int c), which makes a class polymorphic. Overloading only requires the same method name

Rewrite: the subclass redefines the function of the parent class so that the class name and parameter type are the same, thus realizing the polymorphism of the parent class and the subclass. The shell remains the same, the core is rewritten!

The constructor of the parent class cannot be overridden because the constructor of the parent class should be the same as the class name, and if overridden, the subclass name must be the same as the parent class name. Two classes with the same name are not allowed in Java. Benefit: subclasses can implement the methods of the parent class as needed. Note that methods defined as final and static in the parent class cannot be overridden.

You can think of rewriting as a special case of overloading.

That's all for the content of "the difference between overloading and rewriting in java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Methods subclasses class names polymorphism content polymorphism more knowledge utility identity consistency learning and then two functions parameters names dilemmas shells benefits Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Tech Info MySQL macOS vpn