In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the rules for the use of Overriding". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the rules for the use of Overriding".
01. What is rewriting?
Rewriting brings a very important ability to allow subclasses to reimplement methods that inherit from the parent class. In the following figure, Animal is the parent class and Dog is the subclass. Dog reimplements the move () method to distinguish it from the parent class. After all, the dog is quite distinctive in running.
The rewritten method and the overridden method have not only the same method name, but also the same parameters (that is, the same method signature), but the method body is different.
02. Which methods can be rewritten?
Rule 1: only inherited methods can be overridden.
Because rewriting occurs when a subclass reimplements a method inherited from the parent class, it is understandable that only inherited methods can be overridden. This means that only methods modified by public, protected, or default can be overridden, and methods modified by private cannot be overridden.
The Animal class has three methods: move (), eat (), and sleep ():
Public class Animal {public void move () {} protected void eat () {} void sleep () {}}
The Dog class overrides these three methods:
Public class Dog extends Animal {public void move () {} protected void eat () {} void sleep () {}}
OK, no problem at all. But if the method in the parent class is private, it won't work.
Public class Animal {private void move () {}}
At this point, the move () method in the Dog class is no longer an override method, because the move () method of the parent class is private and is not visible to the subclass.
Public class Dog extends Animal {public void move () {}}
03. Which methods cannot be rewritten?
Rule 2: the methods of final and static cannot be rewritten.
A method that is final means that it cannot be inherited by subclasses, so there is no way to override it.
Public class Animal {final void move () {}}
Because move () in the parent class Animal is final, the subclass has a compilation error when it tries to override the method!
Similarly, overrides are not allowed if a method is static, because static methods can be used for all instances of the parent class and subclasses.
Public class Animal {final void move () {}}
The purpose of rewriting is to show polymorphism depending on the type of object, while static methods can be used without creating an object. Without the object, there is no point in rewriting the "type of object" required.
04. Requirements for rewriting methods
Rule 3: overridden methods must have the same parameter list.
Public class Animal {void eat (String food) {}}
The eat () method in the Dog class maintains the same tone of the parent class method eat (), with one parameter-- food of type String.
Public class Dog extends Animal {public void eat (String food) {}}
Once the subclass does not follow this rule, for example, add a parameter:
Public class Dog extends Animal {public void eat (String food, int amount) {}}
This is no longer a rewritten category, and certainly not an overloaded category, because overloading considers the same class.
Rule 4: overridden methods must return the same type.
The parent class has no return type:
Public class Animal {void eat (String food) {}}
The subclass attempts to return String:
Public class Dog extends Animal {public String eat (String food) {return null;}}
So there is an error in the compilation (the return type is incompatible).
Rule 5: overridden methods cannot use permission modifiers with more restrictive levels.
It can be understood like this:
If the overridden method is default, the overridden method can be default, protected, or public.
If the overridden method is protected, then the overridden method can only be protected or public.
If the overridden method is public, then the overridden method can only be public.
For example, the method in the parent class is protected:
Public class Animal {protected void eat () {}}
The method in a subclass can be public:
Public class Dog extends Animal {public void eat () {}}
If the methods in the subclass use stricter permission modifiers, the compiler reports an error.
Rule 6: the overridden method cannot throw a higher-level exception than in the parent class.
For example, if the method in the parent class throws IOException, then the method overridden in the subclass cannot throw Exception, can be a subclass of IOException or does not throw any exceptions. This rule applies only to detectable exceptions.
Checked exceptions must be caught explicitly in the source code, and unchecked exceptions are so-called run-time exceptions, such as NullPointerException, ArrayIndexOutOfBoundsException, and so on, which are not required by the compiler.
The parent class throws IOException:
Public class Animal {protected void eat () throws IOException {}}
The subclass throws a FileNotFoundException that satisfies the rule of rewriting because FileNotFoundException is a subclass of IOException.
Public class Dog extends Animal {public void eat () throws FileNotFoundException {}}
If the subclass throws a new exception, and it is a checked exception:
Public class Dog extends Animal {public void eat () throws FileNotFoundException, InterruptedException {}}
Then the compiler will prompt an error:
Error: (9,16) java: eat () in com.itwanger.overriding.Dog cannot overwrite eat () in com.itwanger.overriding.Animal. The method overridden does not throw java.lang.InterruptedException.
But if the subclass throws a unchecked exception, there is no conflict:
Public class Dog extends Animal {public void eat () throws FileNotFoundException, IllegalArgumentException {}}
If the subclass throws a higher-level exception:
Public class Dog extends Animal {public void eat () throws Exception {}}
The compiler also prompts errors because Exception is the parent class of IOException.
Error: (9,16) java: eat () in com.itwanger.overriding.Dog cannot overwrite eat () in com.itwanger.overriding.Animal. The method overridden does not throw java.lang.Exception.
How do I call the overridden method?
Rule 7: methods that are overridden in the parent class can be called through the super keyword in the subclass.
It is a common practice for a subclass to inherit the method of the parent class instead of reimplementing it, in which case the method of the parent class can be called as follows:
Super.overriddenMethodName ()
Let's look at the example.
Public class Animal {protected void eat () {}}
The subclass overrides the eat () method, and then in the eat () method of the subclass, you can call the method of the parent class through super.eat () on the first line of the method body, and then add your own code.
Public class Dog extends Animal {public void eat () {super.eat (); / / Dog-eat}}
06. Rewriting and construction methods
Rule 8: constructors cannot be overridden.
Because the constructor is special, and the constructor of the subclass cannot have the same name as the constructor of the parent class (the class name is different), there is no relationship between the constructor and the override.
07. Overrides and abstract methods
Rule 9: if a class inherits an abstract class, the abstract methods in the abstract class must be overridden in the subclass.
Let's first take a look at such an interface class:
Public interface Animal {void move ();}
The methods in the interface are abstract methods by default, which can be seen through decompilation:
Public interface Animal {public abstract void move ();}
If an abstract class implements the Animal interface, the move () method does not have to be overridden:
Public abstract class AbstractDog implements Animal {protected abstract void bark ();}
But if a class inherits the abstract class AbstractDog, both the move () method in the Animal interface and the abstract method bark () in the abstract class AbstractDog must be overridden:
Public class BullDog extends AbstractDog {public void move () {} protected void bark () {}}
08, rewriting and synchronized methods
Rule 10: the synchronized keyword has no effect on the rewriting rule.
The synchronized keyword is used to get and release listening objects in a multithreaded environment, so it has no effect on the rewriting rule, which means that the synchronized method can override an asynchronous method.
09, rewriting and strictfp methods
Rule 11: the strictfp keyword has no effect on the rewriting rule.
If you want to make floating-point operations more accurate and will not result in inconsistent results due to different hardware platforms, you can add the strictfp keyword to the method. So the strictfp key has nothing to do with rewriting rules.
Thank you for your reading, the above is the content of "what are the rules for the use of Overriding". After the study of this article, I believe you have a deeper understanding of the rules for the use of Overriding, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.