25. Please describe the difference between interface and abstract class

  1. Abstract class: abstract class refers to what this object is

  2. Interface: Interface refers to what this object can do

    Example: Define a dog class (abstract class): Husky and Golden Retriever belong to the dog class (implementation class) Dog: sleep, dismantle the house... You can define an interface and let the implementation class implement the interface

  3. Therefore, in the Java language, a class can only inherit one class (abstract class) (just as a dog cannot be biological and non-biological at the same time), but interfaces can be inherited multiple times, and one class can implement multiple interfaces.

  4. All methods in the interface are abstract (abstract modification) and an abstract class is a class that declares a method without implementing it.

  5. Interface can be multiple inheritance, abstract class single inheritance

  6. Interface definition methods cannot be implemented, but abstract classes can implement some methods

  7. The basic data type in the interface is static, but the abstract class is not.

Note:
The functions of abstract classes far exceed interfaces, but the cost of defining abstract classes is relatively high. Because of the high-level language (from the actual design), each class can only inherit one class. In this class you must inherit or write out all the commonalities of all its subclasses. Although the interface will weaken a lot in terms of function, but the interface is for the realization of an action, and a class can inherit multiple interfaces, the difficulty in the design will be relatively reduced.

Guess you like

Origin blog.csdn.net/zhu_fangyuan/article/details/108704517