A question that the Java subclass should be aware of when overriding the methods of the parent class

Source: Internet
Author: User

If you want to implement a qualified rewrite method instead of overloading, you must meet the following requirements at the same time!

A, one of the rewrite rules:
The overriding method cannot have a stricter access level than the overridden method limit .
(but more broadly, such as the parent class method is the package access permission, the subclass's overriding method is public access.) For example: The object class has a ToString () method, it is always easy to forget the public modifier when we begin to rewrite this method, and the compiler will of course not miss any chance to teach us. The reason for the error is that there is no access modifier with the package access permissions , package access permissions than the public of course, so the compiler will error.

Anyway, the subclass writes public, it must be true.

b, rewrite the second rule:
The argument list must be the same as the overridden method.
Rewrite has a twin brother called Overload, that is, behind the appearance. If the parameters of the subclass method are different from those of the parent class, then you are mistaken, and that is overloaded, not rewritten.

C, rewrite the third rule:
The return type must be the same as the return type of the overridden method.
The parent class method A:void eat () {} Subclass Method B:int eat () {}, although the parameters are the same, but the return type is different, so it is not overridden.
The parent class method A:int eat () {} Subclass Method B:long eat () {} The return type is compatible with the parent class, but the difference is different, so it is not overridden.

D, rewrite the rule four:
An overriding method cannot throw a new exception or check an exception that is more extensive than the check exception declared by the overridden method. However, you can throw fewer, more limited, or non-throwing exceptions.

1 ImportJava.io.*;2  Public classTest {3    Public Static voidMain (string[] args) {4Animal h =NewHorse ();5    Try {6 h.eat ();7    }8    Catch(Exception e) {9    }Ten  } One } A  - classAnimal { -    Public voidEat ()throwsexception{ theSystem.out.println ("Animal is eating."); -    Throw NewException (); -   } - } +  - classHorseextendsanimal{ +     Public voidEat ()throwsioexception{ ASystem.out.println ("Horse is eating."); at     Throw NewIOException (); -   } -}

In this example, the parent class throws a check exception exception, and the subclass that throws the IOException is a subclass of exception, which is a more limited exception than the overridden method, which is possible. If, in turn, the parent class throws IOException, the subclass throws a more general exception, then it is not compiled.
Note: This limitation is only for checking exceptions, and the runtime exception runtimeexception and its subclasses are no longer in this restriction.

E, rewrite the rules of the Five:
You cannot override a method that is identified as final.

F, rewrite the rule six:
If a method cannot be inherited, it cannot be overridden.

More typically, the private method of the parent class. The next meeting produces an interesting phenomenon.  

1  Public classTest {2    Public Static voidMain (string[] args) {3    //Animal h = new Horse ();4Horse h =NewHorse ();5 h.eat ();6    }7 }8 9 classAnimal {Ten    Private voideat () { OneSystem.out.println ("Animal is eating."); A     } -  } -  the classHorseextendsanimal{ -     Public voideat () { -System.out.println ("Horse is eating."); -    } +}

This code can be compiled. It appears to violate the sixth rule, but it's actually a bit of a coincidence. The Eat () method of the animal class cannot be inherited, so the Eat () method in the horse class is a completely new method, not a rewrite or an overload, but a completely new method that only belongs to the horse class! This makes a lot of people confused, but it is not so difficult to understand.
If this is the main () method:
Animal h = new Horse ();
Horse h = new Horse ();
H.eat ();
The compiler will give an error, why? The Eat () method of the horse class is public! Should be able to call Ah! Keep in mind that polymorphism only looks at the methods referenced by the parent class, not the method of the Subclass Object!

Eric Tsang's insights are as follows

Polymorphic only look at the methods referenced by the parent class, but the method of the sub-class object! What do you mean? Animal h=new Horse (); This embodies polymorphism, However, H.eat () only calls the subclass to rewrite the parent class, and the demo because the parent class method is private, so the subclass cannot override this method, that is, the subclass that Eat () is a subclass of its own newly created method, with the parent class has no relationship, so this time animal h= New Horse ();

A question that the Java subclass should be aware of when overriding the methods of the parent class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.