Java—Abstract class

Table of contents

1. Abstract class concept

2. Abstract class syntax

3. Abstract class characteristics

4. The role of abstract classes


1. Abstract class concept

        In the object-oriented concept, all objects are described by classes, but conversely, not all classes are used to describe objects, if a class does not contain enough information to describe a specific object , such a class is an abstract class. For example:

         

 

         Rectangles, circles and triangles, and graphics are inheritance relationships, dogs and cats, and animals are inheritance relationships, but graphics and animals are not specific graphics and animals, so the internal method bark and method drow cannot be implemented specifically, and their Subclasses can be implemented concretely, and internal methods can be implemented concretely, so Shape and Animals can be designed as abstract classes.

        In the example of printing graphics, we found that the draw method in the parent class Shape does not seem to have any actual work , and the main drawing graphics are done by the draw methods of various subclasses of Shape . Like this without actual work method , we can design it as an abstract method (abstract class  method) , and the class containing the abstract method is called an abstract class (abstract class).

2. Abstract class syntax

        In Java , if a class is modified by abstract , it is called an abstract class, and the method modified by abstract in an abstract class is called an abstract method , and the abstract method does not need to give a specific implementation body .

//Abstract class
public abstract class  Shape{
    public String name;
    //drawing, abstract method, no method body
    public abstract void drowfun();
    
    public double getname(){
           return name;
    }
}

        Note: An abstract class is also a class, which can contain properties, ordinary methods, and even construction methods .

3. Abstract class characteristics

        1. Abstract classes cannot instantiate objects directly

        Shape shape=new Shape();//Compile error

        2. The access qualifier of the abstract method cannot be private

        abstract class Shape {

                abstract private void draw ();
        }//Compile error
        3. Abstract methods cannot be modified by final and static , because abstract methods must be rewritten by subclasses.
           public abstract class Shape {
                abstract final void methodA ();
                abstract public static void methodB ();
        }//Compile error
         4. The abstract class must be inherited, and the subclass must rewrite the abstract method in the parent class after inheritance, otherwise the subclass is also an abstract class and must be modified with abstract .
class Cycle extends Shape{
    @Override
    public void drowfun() {
        System.out.println("Draw a circle⚪~~~");
    }
}

        5.  Abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes .

        6. There can be a construction method in the abstract class for the subclass to initialize the member variables of the parent class when creating an object .

4. The role of abstract classes

        The abstract class itself cannot be instantiated . If you want to use it , you can only create a subclass of the abstract class . Then let the subclass override the abstract method in the abstract class . The scenario of using an abstract class is like the above code. The actual work should not be done by the parent class , but by the subclass . Then if you accidentally misuse it as the parent class at this time, you will not report an error if you use a normal class compiler . But if the parent class is an abstract class, an error will be prompted when instantiating , so we can find the problem as soon as possible .

Guess you like

Origin blog.csdn.net/qq_64668629/article/details/132084833