java: object-oriented-abstract class

How did it happen?

For example, the Cat and Dog classes inherit the pet Pet, then we instantiate ( new Pet), which is actually meaningless, then we can turn Pet into an abstract class.

How to prevent the parent class from being instantiated?

The classes modified by abstract are called abstract classes.

  • Abstract classes cannot be instantiated
public abstract class 类名 {
    
    }

How to ensure that the subclass must override the method of the parent class?

The method modified by abstract is called abstract method.

  • Abstract class methods must be overridden
// 省掉了方法名后面的 {}
public abstract 返回值类型 方法名();

Abstract features

  • Abstract classes cannot be instantiated
  • Abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes.
  • A subclass of an abstract class must give concrete implementations of the abstract methods in the abstract class, unless the subclass is also an abstract class.
  • Construction methods, class methods (methods modified with static) cannot be declared as abstract methods.

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/113837249