In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about rewriting, overloading, overloading, and polymorphic differences. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
Override- > overwrite (= overwrite), overload- > overload, polymorphism-> polymorphism
Override- > rewrite (= overwrite)
Override is rewritten (overridden) by a method to achieve different functions. It is generally used for subclasses to override (reimplement) methods in the parent class when inheriting from the parent class.
Rules for rewriting (overwriting):
1. The parameter list of the overridden method must be exactly the same as that of the overridden method, otherwise it cannot be called rewriting but overloading.
2. The access modifier of the overridden method must be greater than that of the overridden method (public > protected > default > private).
3. The return value of the overridden method must be the same as that of the overridden method
4. The exception thrown by the overridden method must be consistent with the exception thrown by the overridden method, or its subclass
5. The overridden method cannot be private, otherwise a new method is defined in its subclass, and it is not rewritten.
6. Static methods cannot be rewritten as non-static methods (there will be compilation errors).
Overload- > overload
Overload is overloaded and is typically used to implement several overloaded methods within a class with the same name but different parameter forms.
Rules for overloading:
1. When using overloading, it can only be realized by the same method name and different parameter forms. Different parameter types can be different parameter types, different number of parameters, different parameter order (parameter types must be different)
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.
Polymorphism-> polymorphism
The concept of polymorphism is complex and has many meanings. An interesting but not rigorous statement is that inheritance is a method in which a subclass uses a parent class, while polymorphism is a method in which a parent class uses a subclass. In general, we use polymorphism to avoid code bloated and difficult to maintain due to heavy overloading in the parent class.
For example: public class Shape {public static void main (String [] args) {Triangle tri = new Triangle (); System.out.println ("Triangle is a type of shape?" + tri.isShape ()); / / inheritance Shape shape = new Triangle (); System.out.println ("My shape has" + shape.getSides () + "sides."); / / polymorphic Rectangle Rec = new Rectangle (); Shape shape2 = Rec System.out.println ("My shape has" + shape2.getSides (Rec) + "sides."); / / overload} public boolean isShape () {return true;} public int getSides () {return 0;} public int getSides (Triangle tri) {/ / overload return 3;} public int getSides (Rectangle rec) {/ / overload return 4;}} class Triangle extends Shape {public int getSides () {/ / rewrite to implement polymorphic return 3 }} class Rectangle extends Shape {public int getSides (int I) {/ / overloaded return I;}} above is all the content of the article "what is rewriting, overwriting, overloading, and polymorphic differentiation". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.