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

What's the difference between Overload and Override?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is the difference between Overload and Override". In daily operation, I believe many people have doubts about the difference between Overload and Override. 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 questions of "what is the difference between Overload and Override?" Next, please follow the editor to study!

Overriding Override means that a method in a subclass can have exactly the same name and parameters as a method in the parent class. When this method is called through the instance object created by the subclass, the defined method in the subclass will be called, which is equivalent to overwriting the exact same method defined in the parent class, which is also a manifestation of the polymorphism of object-oriented programming. When a subclass overrides the methods of the parent class, it can only throw fewer exceptions than the parent class, or the child exception thrown by the parent class, because the subclass can solve some of the problems of the parent class and cannot have more problems than the parent class. The access rights of subclass methods can only be larger than those of the parent class, not smaller. If the method of the parent class is of type private, then there is no override restriction for the subclass, which is equivalent to the addition of an entirely new method to the subclass.

As for the question of whether the Overloaded method can change the type of return value, it depends on what you want to ask. The subject is very vague. If several Overloaded methods have different parameter lists, their return types can of course be different. But I guess the question you want to ask is: if the parameter lists of the two methods are exactly the same, whether you can overload Overload by making their return values different. This is not possible, we can use absurdity to illustrate this problem, because sometimes we can call a method without defining the return result variable, that is, do not care about the return result. For example, when we call the map.remove (key) method, although the remove method has a return value, we usually do not define the variable that receives the return result. At this point, assuming that there are two methods in the class with exactly the same name and parameter list, but with different return types, java cannot determine which method the programmer wants to call, because it cannot tell by the return result type.

Override can be translated as overwriting. Literally, it overrides a method and rewrites it in order to achieve different effects. The most familiar overlay for us is the implementation of interface methods, which are generally only declared in the interface, but when we implement them, we need to implement all the methods declared by the interface. In addition to this typical usage, we may also override methods in the parent class in the subclass in inheritance. Pay attention to the following points when covering:

1. The logo of the covered method must exactly match that of the covered method in order to achieve the effect of coverage.

2. The return value of the overridden method must be the same as that of the overridden method

3. The exception thrown by the overridden method must be consistent with the exception thrown by the overridden method, or its subclass

4. The overridden method cannot be private, otherwise a new method is defined in its subclass and it is not overridden.

Overload may be familiar to us and can be translated as overloading. It means that we can define some methods with the same name, distinguish these methods by defining different input parameters, and then when called again, VM will choose the appropriate method to execute according to different parameter styles. Note the following when using overloads:

1. Only different parameter styles can be passed when overloading is used. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types within the same method must be different, for example, it can be fun (int,float), but not fun (int,int))

2. Cannot be overloaded by access permission, return type, and thrown exception

3. The exception type and number of methods will not affect the overloading.

4. For inheritance, if a method has access permissions of priavte in the parent class, then it cannot be overloaded in the subclass. If defined, it will only define a new method and will not achieve the effect of overloading.

At this point, the study of "what is the difference between Overload and Override" 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