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 is the difference between Overload and Override developed by Java

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

Share

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

This article mainly explains "What is the difference between Java development Overload and Override", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "What is the difference between Java development Overload and Override"!

What is the difference between Java development override and Override?

Can Overloaded methods change the type of return value? Overload means overload, override means overwrite. Overload means that there can be more than one method with the same name in the same class, but the parameter lists of these methods are different (that is, the number or type of parameters are different).

Overriding means that the method in the subclass can have exactly the same name and parameters as a method in the parent class. When calling this method through an instance object created by the subclass, the defined method in the subclass will be called, which is equivalent to overriding the exactly the same method defined in the parent class. This is also a manifestation of polymorphism in object-oriented programming. Subclasses that override the parent class's methods can throw fewer exceptions than the parent class, or child exceptions that the parent class throws, because the child class can solve some of the parent class's problems and cannot have more problems than the parent class. Subclass methods can only have greater access rights than their parent class, not less. If the method of the parent class is of type private, then the subclass has no override restrictions, which is equivalent to adding a completely new method to the subclass.

As for whether Overloaded methods can change the type of return value, it depends on what you want to ask. The title is vague. If several Overloaded methods have different parameter lists, their returner types can of course be different. But I guess the question you want to ask is: If the parameter lists of two methods are exactly the same, can you overload Overload by making their return values different? This is not possible, we can use the argument to explain this problem, because we sometimes call a method can also not define the return result variable, that is, do not care about its return result, for example, we call map.remove(key) method, although the remove method has a return value, but we usually do not define the variable to receive the return result, then assume that there are two methods in the class with exactly the same name and parameter list, only the return type is different, Java can't determine which method the programmer wants to call because it can't tell by the return type.

Override can be translated as overriding, literally knowing that it overrides a method and overrides it to achieve a different effect. The most familiar overlay to us is the implementation of interface methods, which are generally only declared in the interface, and we need to implement all the methods declared by the interface when implementing them. In addition to this typical usage, we may also override methods in parent classes in a subclass in inheritance. The following points should be noted in coverage:

1. The flag of the covered method must match the flag of the covered method completely in order to achieve the effect of coverage.

The return value of the overridden method must be consistent with the return value of the overridden method.

The exception thrown by the overriding method must be consistent with the exception thrown by the overridden method, or a subclass thereof;

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

overload may be familiar to us, it can be translated as overload, it means that we can define some methods with the same name, by defining different input parameters to distinguish these methods, and then when invoked, VM will choose the appropriate method to execute according to different parameter styles. In the use of overload to note the following points:

1. When overloading is used, different parameter styles can only be adopted. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types in the same method must be different, for example, fun(int,float), but not fun(int,int));

2, can not be overloaded by access permissions, return types, thrown exceptions;

The exception type and number of methods do not affect overloading.

4. For inheritance, if a method has priavte access rights in the parent class, it cannot be overloaded in the child class. If it is defined, it only defines a new method and will not achieve the effect of overloading.

At this point, I believe that everyone has a deeper understanding of "Java development Overload and what is the difference between Override", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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