In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what are the differences between Overload and Override, I believe that most people still do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Overload means overloading and Override means overwriting, that is, rewriting.
Overloading Overload means that there can be multiple methods with the same name in the same class, but each of these methods has a different parameter list (that is, a different number or type of parameters).
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, which 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 is priavte in the parent class, it cannot be overloaded in the subclass, and if defined, it will only define a new method and will not achieve the overloading effect.
In short, rewriting Overriding is a manifestation of polymorphism between parent and child classes, and overloading Overloading is a manifestation of polymorphism in a class. It is important that the method of Overloaded is to change the type of return value.
The above is all the content of the article "what's the difference between Overload and Override". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.