The conditions that the Java subclass must meet to override the parent class method

Source: Internet
Author: User
Tags instance method

First, describe

subclasses override (overwrite) the criteria that a method of the parent class must satisfy:
1. The methods in the parent class must be visible in the subclass, that is, the subclass inherits the method from the parent class (you can explicitly use the Super keyword to access the overridden method in the parent class), and if the method in the parent class is private, the subclass cannot inherit or overwrite.
2. The method of the subclass and parent class must be an instance method, if the parent class is a static method and the subclass is an instance method, or the opposite will be an error. If both the parent and child classes are static methods, the subclass hides the method of the parent class instead of overriding the parent class method.
3. The methods of the subclass and the parent class must have the same function name, a parameter list, and the child class's return value is the same as the parent class or a subtype of the parent class return type (after jdk1.5). If the method name is the same and the argument list is different (the return type can be the same or different), it is only an overload of the method, not an override. If the method name and argument list are the same, the return value type is different, and the subclass return value type is not a subclass of the parent class return value type, the compiler will error.

4. The access rights of the subclass method cannot be less than the access rights of the parent class method (you can have the same access rights or the child class has greater access than the parent class). Access rights are high to low: public, protected, package access, private. If the access rights of the subclass method are lower than the parent class, the compiler gives an error message
5. A subclass method cannot throw more compile-time exceptions (not run-time exceptions) than a parent class method, that is, a compile-time exception thrown by a subclass method, or a subclass that is the same as, or is, the parent class exception.

Second, the source code

Package Tong.yue.day4_264;import Java.io.filenotfoundexception;import Java.io.ioexception;import Javax.swing.jbutton;import javax.swing.jcomponent;/** * Subclass Overrides (Overrides) the method of the parent class must satisfy the condition: * 1. Methods in the parent class must be visible in the child class, That is, the subclass inherits the method from the parent class (you can explicitly use the Super keyword to access the overridden method in the parent class), and if the method in the parent class is private, the subclass cannot inherit or overwrite. * 2. The method of the subclass and parent class must be an instance method, if the parent class is a static method and the subclass is an instance method, or the opposite will be an error. * If both the parent and child classes are static methods, the subclass hides the method of the parent class instead of overriding the parent class method. * 3. The methods of the subclass and the parent class must have the same function name, a parameter list, and the child class's return value is the same as the parent class or a subtype of the parent class return type (after jdk1.5). * If the method name is the same and the argument list is different (the return type can be the same or different), then the method is overloaded, not overridden. * If the method name and argument list are the same, the return value type is different, and the subclass return value type is not a subclass of the parent class return value type, the compiler will error. * 4. The subclass method cannot access permissions that are less than the access rights of the parent method (you can have the same access rights or the subclass's access is greater than the parent class) * Access rights are high to low: public, protected, package access, private. If the subclass method has less access than the parent class, the compiler gives an error message * 5. The subclass method cannot throw more compile-time exceptions (not run-time exceptions) than the parent class method, that is, a compile-time exception thrown by a subclass method, or a subclass that is the same as, or is, the parent class exception. * @author Tong * */public class Inheritance {/** * @param args */public static void main (string[] args) {//TODO Auto-gene Rated method Stub}}class pareant {public void dosomething () {}public static void DoSomething1 () {}public jcomponent dosome Thing2 () {return null;}public jcomponent DoSomething3 () {return null;} public void DoSomething4 (int x) {}public void DoSomething5 () throws filenotfoundexception{}private void DoSomething6 (int x) {}}class Sub extends Pareant {///Parent class The method is private, the subclass is not visible, so subclasses cannot inherit and overwrite the private method of the parent class//here does not overwrite the private method of the parent class, but instead creates a new method in the subclass with the same name as the parent class. These two methods have nothing to do with the private void DoSomething6 (int x) {}//error, the parent class is an instance method, the subclass is a static method,//this the statics methods cannot hide the instance method fr Om pareantpublic static void DoSomething () {}//error, parent class is static method, subclass is instance method//this static method cannot hide the instance method fro M pareantpublic void DoSomething1 () {}//is correct, the return value of the parent class is jcomponent, which is the parent class of the subclass return value class. Starting with jdk1.5, the return value when overriding a parent class method can be the same as the parent class, or it can be a subclass of the parent class public JButton doSomething2 () {return null;} Error, the return value of the parent class is jcomponent, the subclass return value type is int, the subclass return value differs from the parent class return value type, and is not a subclass of the parent class return value//the return type is incompatible with PAREANT.DOSOMETHING3 () public int doSomething3 () {return 1;} Error, the parent class access is public, the subclass is protected, the child class access permissions can not be lower than the parent class, may be higher than or equal to the parent class access rights//cannot reduce the visibility of the inherited method from pareantprotected void DoSomething4 (iNT x) {}//error, the subclass can not throw more exceptions than the parent class, the parent throws the FileNotFoundException exception, the subclass throws the IOException is the parent class exception of the parent class//subclass can only throw the same exception as the parent class, or a subclass of the parent class exception, and cannot exceed the parent class//exception IOException is not compatible with throws clause in Pareant.dosomething5 () public void doSomething5 () th Rows ioexception{}}


The conditions that the Java subclass must meet to override the parent class method

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.