What is the difference between an interface and an abstract class?
What is the basis for using interfaces and abstract classes?
The concepts of interfaces and abstract classes are different. The interface is the abstraction of actions, and the abstract class is the abstraction of the source.
The abstract class indicates what this object is. The interface indicates what the object can do. For example, men and women (if they are classes ......), Their abstract class is human. It means they are all people.
People can eat, dogs can also eat, you can define "eat" as an interface, and then let these classes implement it.
Therefore, in advanced languages, a class can only inherit one class (abstract class) (just as humans cannot be both biological and non-biological ), however, multiple interfaces (meal and walking interfaces) can be implemented ).
First, the interface is a variant of the abstract class, and all the methods in the interface are abstract. Abstract classes are classes that declare the existence of methods rather than implementing them.
Second, the interface can be inherited more than the abstract class.
Third, the interface definition method cannot be implemented, while the abstract class can implement some methods.
Fourth, the basic data type in the interface is static, but the image is not.
When you focus on the essence of a thing, use abstract classes; when you focus on an operation, use interfaces.
Abstract classes have far more functions than interfaces. However, it is costly to define abstract classes. Because in advanced languages (also in actual design), each class can only inherit one class. In this class, you must inherit or compile
All commonalities. Although the interface is much weaker in function, it only describes an action. In addition, you can implement multiple interfaces in a class at the same time. This will reduce the difficulty in the design phase.