Abstract classes in Java (just read this article)

In Java, an abstract class is a class that cannot be instantiated. It is usually used as a base class for other classes to provide some common behaviors and structures. Here is some important information about Java abstract classes:

  1. Define abstract class :

    Use abstractthe keyword to define abstract classes.

    abstract class MyAbstractClass {
          
          
        // 声明抽象方法(没有方法体)
        abstract void myAbstractMethod();
        
        // 具体方法也可以存在
        void myConcreteMethod() {
          
          
            System.out.println("Concrete method in abstract class");
        }
    }
    
  2. Inherit abstract class :

    A subclass can inherit an abstract class, but if the subclass does not implement all the abstract methods in the abstract class, then the subclass must also be declared as an abstract class.

    class MyConcreteClass extends MyAbstractClass {
          
          
        @Override
        void myAbstractMethod() {
          
          
            System.out.println("Implementing myAbstractMethod");
        }
    }
    
  3. Abstract method :

    Abstract classes can contain abstract methods. These methods do not have method bodies, they are just declarations and need to be implemented in subclasses.

    abstract void myAbstractMethod();
    
  4. The difference between abstract class and interface :

    • An abstract class can contain both abstract and concrete methods, while an interface can contain only abstract methods.
    • A class can only inherit from one abstract class, but can implement multiple interfaces.
    • The purpose of abstract classes is to establish inheritance relationships between classes, while the purpose of interfaces is to implement multiple inheritance.
  5. Practical applications of abstract classes :

    • Abstract classes are used to establish a class hierarchy and provide common behaviors and attributes. The specific implementation is completed by subclasses.
    • When multiple classes have similar behaviors and properties, their common parts can be abstracted into an abstract class to improve code reusability and maintainability.
  6. Constructor of abstract class :

    Abstract classes can have constructors but cannot be instantiated. When a subclass is instantiated, it calls the constructor of the parent class.

    abstract class MyAbstractClass {
          
          
        int x;
        
        MyAbstractClass(int x) {
          
          
            this.x = x;
        }
        
        abstract void myAbstractMethod();
    }
    
  7. Static members of abstract classes :

    Abstract classes can contain static members (static fields and static methods), which have the same nature as static members of ordinary classes.

    abstract class MyAbstractClass {
          
          
        static int staticField = 10;
        
        static void staticMethod() {
          
          
            System.out.println("Static method in abstract class");
        }
    }
    

Abstract classes are an important part of object-oriented programming in Java. They provide a way to design and organize class hierarchies while also providing a certain degree of code reusability.

Guess you like

Origin blog.csdn.net/yang_guang3/article/details/133298344