Java Object Oriented 07 (Abstract Class)

The abstract modifier can be used to modify methods and classes. If a method is modified, the method is an abstract method, and if a class is modified, the class is an abstract class.

Common methods can exist in abstract classes, but the class i where the abstract methods are located must be declared as abstract classes

Abstract classes cannot use the new keyword to create objects, they can only be used through subclass inheritance

Abstract methods have only method declarations, no method implementations, they are used to allow subclasses to implement

If the subclass inherits the abstract class, then the abstract method in the abstract class must be rewritten. Unless the subclass is also an abstract class, the subclass will override the abstract method

Code example:

Action

package com.oop.demo08;

//abstract抽象类
public abstract class Action {
    //抽象方法,只有方法名没有方法内容,由子类重写方法
    public abstract void dosomething();
}

A

package com.oop.demo08;

//继承了抽象类的所有子类必须重写抽象类中的抽象方法,除非继承该抽象类的子类也是抽象类,则由子子类重写方法
public class A extends Action{
    @Override
    public void dosomething() {

    }
}

The role of abstract classes: prevent duplication of code, improve development efficiency

Guess you like

Origin blog.csdn.net/qq_51224492/article/details/113985540