Object-Oriented Programming (Advanced) 3: Method Rewriting

Table of contents

3.1 Example of method rewriting

@Override Instructions for use:

3.2 Requirements for method rewriting

3.3 Summary: method overloading and rewriting

(1) In the same class

(2) In the parent-child class

3.4 Exercises


All methods of the parent class will be inherited by the subclass, but when a method is inherited to the subclass, the subclass feels that the original implementation of the parent class is not suitable for its current class, what should it do? Subclasses can transform the methods inherited from the parent class, which we call methods 重写 (override、overwrite). Also known as method's 重置,覆盖 .

When the program is executed, the method of the subclass will override the method of the parent class.

3.1 Example of method rewriting

For example, the new mobile phone adds the function of displaying the avatar of the caller, the code is as follows:

package com.atguigu.inherited.method;

public class Phone {
    public void sendMessage(){
        System.out.println("发短信");
    }
    public void call(){
        System.out.println("打电话");
    }
    public void showNum(){
        System.out.println("来电显示号码");
    }
}

package com.atguigu.inherited.method;

//SmartPhone:智能手机
public class SmartPhone extends Phone{
    //重写父类的来电显示功能的方法
	@Override
    public void showNum(){
        //来电显示姓名和图片功能
        System.out.println("显示来电姓名");
        System.out.println("显示头像");
    }
    //重写父类的通话功能的方法
    @Override
    public void call() {
        System.out.println("语音通话 或 视频通话");
    }
}
package com.atguigu.inherited.method;

public class TestOverride {
    public static void main(String[] args) {
        // 创建子类对象
        SmartPhone sp = new SmartPhone();

        // 调用父类继承而来的方法
        sp.call();

        // 调用子类重写的方法
        sp.showNum();
    }
}

@Override Instructions for use:

Written on the method, it is used to detect whether the requirements of the rewritten method are met. Even if this annotation is not written, as long as the requirements are met, it is the correct way to overwrite and rewrite. It is recommended to keep it, so that the compiler can help us check the format, and it can also make it clear to programmers who read the source code that this is an overridden method.

3.2 Requirements for method rewriting

  1. The method overridden by the subclass has the same ,必须 as the overridden method of the parent class .方法名称参数列表

  2. The return value type of the overridden method of the subclass 不能大于The return value type of the overridden method of the parent class. (eg: Student < Person).

Note: If the return value type is a basic data type and void, it must be the same

  1. Methods overridden by subclasses use the access rights 不能小于of overridden methods of the parent class. (public > protected > default > private)

Note: ① The private method of the parent class cannot be overridden; ② The default method of the cross-package parent class cannot be overridden

  1. The exception thrown by the subclass method cannot be greater than the exception of the overridden method of the parent class

In addition, the methods with the same name and parameters in the subclass and the parent class must be declared as non-static (that is, rewriting) or static (not rewriting) at the same time. Because the static method belongs to the class, the subclass cannot override the method of the parent class.

3.3 Summary: method overloading and rewriting

Method overloading: the method name is the same, but the formal parameter list is different. regardless of the return type.

Method overriding: see above.

(1) In the same class

package com.atguigu.inherited.method;

public class TestOverload {
    public int max(int a, int b){
        return a > b ? a : b;
    }
    public double max(double a, double b){
        return a > b ? a : b;
    }
    public int max(int a, int b,int c){
        return max(max(a,b),c);
    }
}

(2) In the parent-child class

package com.atguigu.inherited.method;

public class TestOverloadOverride {
    public static void main(String[] args) {
        Son s = new Son();
        s.method(1);//只有一个形式的method方法

        Daughter d = new Daughter();
        d.method(1);
        d.method(1,2);//有两个形式的method方法
    }
}

class Father{
    public void method(int i){
        System.out.println("Father.method");
    }
}
class Son extends Father{
    public void method(int i){//重写
        System.out.println("Son.method");
    }
}
class Daughter extends Father{
    public void method(int i,int j){//重载
        System.out.println("Daughter.method");
    }
}

3.4 Exercises

**Exercise 1:**If a method of the parent class is now defined as private access, and this method is declared as default access in the subclass, is this still called rewriting? (NO)

**Exercise 2: **Modify the class Kids defined in Exercise 2 of the inherited content, redefine the employee() method in Kids, override the employee() method defined in the parent class ManKind, and output "Kids should study and no job. "

Guess you like

Origin blog.csdn.net/swx595182208/article/details/129999632