Java learning road-abstract class and abstract method

Java learning road-abstract class and abstract method

Overview

Abstraction is the process of extracting common and essential characteristics from many things and discarding their non-essential characteristics. To be more specific, abstraction is based on the practice of people, through the process of removing roughness and essence, removing falsehoods and keeping the truth, forming concepts, judgments, reasoning and other thinking forms to reflect things. Essential and regular methods.

Abstract classes and abstract methods in Java programs:

  • Abstract methods only need to be declared but not implemented. Abstract methods only declare the returned data type, method name and required parameters, without method body;
  • Abstract classes cannot be instantiated and can only be inherited by subclasses. The inherited subclass must implement the abstract methods defined in the abstract class.

The purpose of abstraction is to extract the same but uncertain things for future reuse. The purpose of defining an abstract class is to implement abstract methods in subclasses.

A class with abstract methods is an abstract class (but abstract classes may not contain abstract methods), and abstract classes must be declared using the abstract keyword.

Example

// 定义一个抽象类
abstract class Demo0 {
    
    
    //普通方法
    public void fun(){
    
    
        System.out.println("普通方法");
    }

    //抽象方法
    public abstract void eat();
}

// 子类继承抽象类
class Demo1 extends Demo0 {
    
    
    // 重写抽象方法
    @Override
    public void eat() {
    
    
        System.out.println("eat() 方法已经实现");
    }
}

to sum up

Abstract class:

  • Abstract classes cannot be instantiated. Because the method in the abstract class is not materialized, this is an incomplete class, so it is meaningless to instantiate it directly;
  • The use of abstract class must have subclasses, use extends inheritance, a subclass can only inherit one abstract class;
  • The subclass (if it is not an abstract class) must override all abstract methods in the abstract class (if the subclass does not implement the abstract methods of the parent class, the subclass must also be defined as an abstract class.) ;
  • An abstract class may not contain abstract methods, but if the class contains abstract methods, the class must be declared as an abstract class;
  • Abstract classes cannot use final declarations, because abstract classes must have subclasses, and classes defined by final cannot have subclasses;
  • Since there will be some attributes in the abstract class, there must be a construction method in the abstract class, whose purpose is to initialize the attributes. And when the subclass object is instantiated, it still satisfies the order of performing the superclass construction first, and then the subclass construction;
  • External abstract classes are not allowed to use static declarations, while internal abstract classes can use static declarations.

Abstract method:

  • Abstract method refers to a method without a method body, and the abstract method must also be modified with the keyword abstract;
  • ** The abstract method must be public or protected ** (because if it is private, it cannot be inherited by the subclass, and the subclass cannot implement the method). The default is public by default.

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/112631685