Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The difference between java rewriting and reloading

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the difference between java rewriting and reloading". In daily operation, I believe that many people have doubts about the difference between java rewriting and reloading. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "the difference between java rewriting and reloading". Next, please follow the editor to study!

Overloading (Overload): polymorphism at compile time

Multiple methods with the same name are defined in a class, but each method is required to have a different type or number of parameters.

Features:

Same method name, = different parameter list = (number of parameters, parameter type, parameter order)

The return value type can be the same or different.

Different exceptions can be thrown

Application:

Mostly used for overloading of construction methods

Class OverloadTest {/ / it is OK to have different parameter order when overloaded, but if the parameter type is different, public static void method (String sinstruction int a) {System.out.println ("method 1");} public static void method (int a line string) {System.out.println ("method 2") } / / overloading of constructor OverloadTest (int vMagneString s) {System.out.println ("constructor 1");} OverloadTest (String sreint v) {System.out.println ("constructor 2");}} rewriting (override, Override): runtime polymorphism

When the subclass inherits the parent class, if you want to modify it, you can use the method override

Features:

Method name, same parameter list

Return value of the same type

The access qualifier cannot be more stringent than the overridden method of the parent class, that is, the scope of access of the overridden method of the subclass is the same or wider than that of the parent class, and cannot be narrower.

You cannot throw a new checked exception or a broader checked exception, you can throw a non-checked exception

Methods modified by final cannot be overridden

Methods modified by static cannot be overridden

Application:

Applied when a subclass needs to modify a method inherited from the parent class

Class OverrideTest {public void method () {System.out.println ("method of parent class");}} class Son extends OverrideTest {@ Override// subclass can add this annotation when overriding parent class method, which automatically verifies whether the rewriting format is correct public void method () {System.out.println ("method of subclass rewriting"). } public static void methodB () {System.out.println ("subclass method");}} public class OverrideOrOverloadDemo {public static void main (String [] args) {new OverrideTest () .method (); new Son () .method (); OverrideTest ot = new Son (); / / ot.methodB () / / subject to the reference, the reference is OverrideTest. There is no method methodB () in this class, so the compilation error ot.method () will be reported; / / when the subclass overrides the method of the parent class, the overridden method of the subclass}} is called

Running result:

At this point, the study of "the difference between java rewriting and reloading" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report