Home  >  Article  >  Java  >  How to Master Method Overriding in Java

How to Master Method Overriding in Java

PHPz
PHPzforward
2023-05-25 13:07:16918browse

How to Master Method Overriding in Java

1. Meaning

After a subclass inherits a parent class, it can write a method in the subclass with the same name and the same parameters as the parent class, so as to realize the control of the parent class. We call this process overwriting of methods with the same name and the same parameters as method override

2. Why use method override

2.1 When the method of the parent class cannot satisfy the needs of the child When meeting the needs of a class, the method needs to be rewritten in the subclass

2.2 Question and Analysis

For example, there is a parent class Peple, a subclass Chinese, and there is a say in the parent class () method, output people talking, but when I want to call the subclass, it outputs Chinese people talking. Obviously calling the method directly cannot work, so I need to rewrite the say method in the subclass

2.3 Sample code

People class

public class Peple {
    private String name;
    private String sex;
    private int age;
    private int weight;

    public Peple() {
    }

    public Peple(String name, String sex, int age, int weight) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.weight=weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }
    public void say(){
        System.out.println("人在说话");
    }

}

Chinese class

public class Chinese extends Peple{
    public Chinese() {
    }
    @Override
    public void say() {
        System.out.println("中国人在说话");
    }
}

Test03 class

public class Test03 {
    public static void main(String[] args) {
        Chinese c=new Chinese();
        c.say();
        //当进行方法重写时,调用的是子类的say()方法
    }
}

2.4 Sample code running screenshot

How to Master Method Overriding in Java

3. How to use method rewriting

3.1 Basic syntax

@Override
权限修饰符 返回值类型 方法名(形参列表){
    //子类重写的方法的权限修饰符的访问权限必须大于等于父类的,但是父类不能是private
    //当父类的返回值类型为基本数据类型或者为void时,子类方法的返回值类型也应该为对应的基本数据类型或者void
  
}

3.2 Specific analysis

3.2.1 The access rights of methods overridden by subclasses should be greater than or equal to the access rights of parent class methods

a Sample code

People class

public class Peple {
    private String name;
    private String sex;
    private int age;
    private int weight;

    public Peple() {
    }

    public Peple(String name, String sex, int age, int weight) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.weight=weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }
    //没有写访问权限的话,默认是default访问权限
    void say(){
        System.out.println("人在说话");
    }

}

Chinese class

public class Chinese extends Peple{
    public Chinese(){
    }
    //父类say方法的访问权限为default,子类say方法的访问权限为public,
    // 符合子类方法访问权限大于等于父类方法访问权限的要求
    @Override
    public void say() {
        System.out.println("中国人在说话");
    }
}

Test03 class

public class Test03 {
    public static void main(String[] args) {
        Chinese c=new Chinese();
        c.say();
    }
}

b Sample code running screenshot

How to Master Method Overriding in Java

If the return value type of the parent class method is a basic data type, then The return value type of the method overridden by the subclass must also be the corresponding basic data type

a Sample code

People class

public class Peple {
    private String name;
    private String sex;
    private int age;
    private int weight;

    public Peple() {
    }

    public Peple(String name, String sex, int age, int weight) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.weight=weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }
    public double add(int a,int b){
       return a+b;
    }

}

Chinese class

public class Chinese extends Peple{
    public Chinese(){
    }
    @Override
    public double add(int a,int b) {
       return a+b+1;
    }
}

Test03 Class

public class Test03 {
    public static void main(String[] args) {
        Chinese c=new Chinese();
        System.out.println("求和之和再加上1的结果为: "+c.add(2, 3));
    }
}

b Sample code running screenshot

How to Master Method Overriding in Java

##3.2.3 When the return value type of the parent class method is void, the subclass overrides The return value type of the method is also void

a Sample code

People class

public class Peple {
    private String name;
    private String sex;
    private int age;
    private int weight;

    public Peple() {
    }

    public Peple(String name, String sex, int age, int weight) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.weight=weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }
    public void eat(){
        System.out.println("人的主食一般是熟食");
    }

}

Chinese class

public class Chinese extends Peple{
    public Chinese(){
    }
    @Override
    public void eat() {
        System.out.println("中国人的主食是大米或者小麦");
    }
}

Test03 class

public class Test03 {
    public static void main(String[] args) {
        Chinese c=new Chinese();
        c.eat();
    }
}

b Screenshot of sample code running

How to Master Method Overriding in Java

3.2.4 When the method of the parent class is modified by static, the subclass cannot override the method

a Error sample code

People class

public class Peple {
    private String name;
    private String sex;
    private int age;
    private int weight;

    public Peple() {
    }

    public Peple(String name, String sex, int age, int weight) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.weight=weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }
    public static void eat(){
        System.out.println("人的主食一般是熟食");
    }

}

Chinese class

public class Chinese extends Peple{
    public Chinese(){
    }
    @Override
    public void eat() {
        System.out.println("中国人的主食是大米或者小麦");
    }
}

Test03 class

public class Test03 {
    public static void main(String[] args) {
        Chinese c=new Chinese();
        c.eat();
    }
}

b Sample code running screenshot

idea when compiling The error message given

How to Master Method Overriding in Java

The error message given after forced operation

How to Master Method Overriding in Java

3.3 Method rewriting Some tips

3.3.1 Copy method

Steps

1. First directly copy (Ctrl C) the one in the parent class that needs to be overridden by the subclass Method

2. Paste (Ctrl V) into the subclass

3. Modify the functions in the subclass to facilitate the realization of requirements that cannot be achieved by the parent class

Operation screenshot display

How to Master Method Overriding in Java

How to Master Method Overriding in Java##3.3.2 Compiler prompt method

Steps

1. First subclass In the class body, in a non-method position, write an English @ symbol

2. Select Overide/implement methods in the prompt...

3. Double-click and an override method selection list pops up. Window

4. Select the corresponding method that needs to be rewritten according to the prompts

5. After clicking the ok button, an overriding method of the method you selected will be generated in the subclass

6. Remove the automatically generated first line in the generated rewrite method, and then write appropriate statements in the method body according to the requirements

Operation screenshot display

How to Master Method Overriding in Java

How to Master Method Overriding in Java

How to Master Method Overriding in Java3.3.3 Shortcut key method

Steps

1. Move the mouse to the override method The position that should be generated

2. Press the Alt key and the Insert key on the keyboard at the same time,

3. In the pop-up box, select Override Methods

4. After double-clicking An interface will pop up, select the method that needs to be overridden by the subclass in the interface

5. After clicking the OK button, the required overriding method will be generated

6. Remove the automatic in the overriding method The first line generated, then write the appropriate statement in its method body

Operation screenshot display

How to Master Method Overriding in Java

How to Master Method Overriding in Java

How to Master Method Overriding in Java

The above is the detailed content of How to Master Method Overriding in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete