17. A method of object-oriented rewriting -----

First, the definition

Subclass inherits the parent class later, the same parameters of the same name can be a method of the parent class, overwrite operation.

Second, the requirement

A method Subclasses override method must have the same name, and parameter list of the parent class is rewritten
2. Subclasses override type of the return value is greater than the parent class is not overwritten return type
3. The access method used in a subclass overrides not less than access to the parent class method to be rewritten
4. The subclass can override the parent class declared as private methods permissions
The subclass of an exception thrown exception is not greater than the parent class is rewritten method

note:

The method is non-static rewriting for the method declared as static is not called rewriting, because static method belonging to the class, parent class subclass can not be covered.

Third, the code

public class test {
    public static void main(String[] args) {
        Student s = new Student();
        s.eat();//学生吃饭
        Person p = new Person();
        p.eat();//人吃饭
    }
}

class Person{
    String name;
    int age;

    public void eat(){
        System.out.println("人吃饭");
    }
}

class Student extends Person{
    String school;

    public voidEAT () { 
        System.out.println ( "Student eating" ); 
    } 
}

 

 

Author: Java beauty

Date: 2020-03-29

Guess you like

Origin www.cnblogs.com/897463196-a/p/12590471.html