What is the difference between abstract class and interface in Java interview questions?

What is the difference between abstract class and interface?

Insert picture description here
1. Similarities:

  • Both are abstract classes and cannot be instantiated.
  • Both the interface implementation class and the subclasses of the abstract class must implement the declared abstract methods.

2. Differences:

  • Interface needs to be implemented, and implements, and abstract class needs to be inherited, and extends.
  • A class can implement multiple interfaces, but a class can only inherit one abstract class.
  • The interface emphasizes the realization of specific functions, and the abstract class emphasizes the ownership relationship.
  • Although the interface implementation class and the subclasses of the abstract class must implement the corresponding abstract methods, the implementation forms are different. Each method in the interface is an abstract method (after JDK1.8, the default and static keywords will be used in the interface. The common methods defined in the modified interface are just declared (declaration, no method body), and the implementation class must be implemented. The subclasses of abstract class can be implemented selectively.

Guess you like

Origin blog.csdn.net/weixin_44296929/article/details/108322637