What is an abstract class in java

abstract class

  • Abstract class
    1. In java, abstract means abstract, which can be used to modify classes and member methods.
    2. Abstract modified classes, this class is an abstract class; abstract modified methods, this method is an abstract method
    tips: The method signature after the abstract method cannot be declared method body
public abstract class Animal{
    
    
	public abstract void run();
}
  • Scenarios for using abstract classes
    1. Abstract classes can be understood as incomplete design drawings. Generally, they are used as parent classes to allow classes to inherit.
    2. When the parent class knows that the subclass must complete certain behaviors, but each subclass implements the behavior It is different, so the parent class defines the behavior as an abstract method, and the specific implementation is handed over to the subclass to complete. At this time, the class can be declared as an abstract class

  • Characteristics of abstract classes
    1. The class has members (member variables, methods, constructors), and abstract classes also have
    2. Abstract classes do not necessarily have abstract methods. A class with abstract methods must be an abstract class
    3. A class inherits An abstract class must rewrite all the abstract methods of the abstract class, otherwise this class must also be defined as an abstract class
    4. You cannot use abstract to modify variables, code blocks, and constructors

  • What is the relationship between final and abstract?
    1. It is a mutually exclusive relationship. 2. The abstract class defined by abstract is used
    as a template for subclasses to inherit, and the class defined by final cannot be inherited.
    The method cannot be overridden

  • example

public abstract class AbstractTemplate <T> {
    
    
    protected void validateParam(T request) {
    
    
        Out.out("father validateParam: " + request.toString());
    }

    public abstract void doExecute(T req) throws Exception;  //抽象方法

    public void execute(T req) {
    
    
        try {
    
    
            //1.参数校验
            validateParam(req);

            //2.执行业务逻辑
             doExecute(req);
        } catch (Exception e) {
    
    
            // 未知异常处理
            Out.out(e);
        }
    }


    public static void main(String[] args) {
    
    
        AbstractTemplate <String>  templateStr = new AbstractTemplate<String>() {
    
    
            @Override  //重写父类的方法
            public void validateParam(String str){
    
    
                super.validateParam(str);
                Out.out("child validateParam: " + str);
            }


            @Override //重写抽象方法
            public void doExecute(String req) throws Exception {
    
    
                Out.out("child doExecute");
            }
        };

        templateStr.execute("ziyuan");

        AbstractTemplate<Integer>  templateInt = new AbstractTemplate<Integer>() {
    
    

            @Override  //重写父类的方法
            public void validateParam(Integer num){
    
    
                super.validateParam(num);
                Out.out("child validateParam: " + num);
            }


            @Override //抽象方法
            public void doExecute(Integer num) throws Exception {
    
    
                Out.out("child doExecute");
            }
        };
        templateInt.execute(8);

    }

}

Output result:

ziyuan 2023 2023-04-13 16:23:44 father validateParam: ziyuan
ziyuan 2023 2023-04-13 16:23:44 child validateParam: ziyuan
ziyuan 2023 2023-04-13 16:23:44 child doExecute
ziyuan 2023 2023-04-13 16:23:44 father validateParam: 8
ziyuan 2023 2023-04-13 16:23:44 child validateParam: 8
ziyuan 2023 2023-04-13 16:23:44 child doExecute

Guess you like

Origin blog.csdn.net/qq_15283475/article/details/130132783