In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what is the difference between Overload and Override in Java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
First of all, we need to understand two concepts: Overload means overload and Override means override, 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.
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 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.
This is the end of the article on "what is the difference between Overload and Override in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.