The difference between ordinary class and abstract class, interface and abstract class

Question 1: What is the difference between ordinary classes and abstract classes?

Abstract class 不能被实例化;
an abstract class can have 抽象方法, 只需申明,无须实现;
a class with abstract methods 一定is an abstract class;
a subclass of an abstract class must implement all the abstract methods in the abstract class, otherwise the subclass is still an abstract class;
abstract methods 不能声明为静态cannot be static、finalmodified.

Question 2: What is the difference between an interface and an abstract class?

(1) Interface

Interface uses interfacemodification;
interface 不能实例化;
class can implement 多个interface;

①Before java8, the methods in the interface were all abstract methods, which were omitted public abstract. ②After java8; static methods can be defined in the interface, 静态方法必须有方法体and ordinary methods have no method body and need to be implemented;

(2) Abstract class

Abstract classes use abstractmodification;
abstract classes 不能被实例化;
abstract classes can only 单继承;
abstract classes can contain abstract methods and non-abstract methods, 非抽象方法需要有方法体;
if a class inherits an abstract class, ① if all abstract methods are implemented, the subclass may not be an abstract class; ②If 没有实现所有的抽象方法, 子类仍然是抽象类.

Guess you like

Origin blog.csdn.net/m0_48170265/article/details/130074699