What is the difference between interface and abstract class

What is the difference between interface and abstract class

In the application scenario, what is the basis for choosing to use interfaces and abstract classes?


First, the similarities and differences between abstract classes and interfaces in syntax:

1. The same

can't be instantiated

2. Differences     

The first point. An interface is a variant of an abstract class. An interface is more abstract than an abstract class. All methods in an interface are abstract.
Second point. Each class can only inherit one abstract class, but can implement multiple interfaces
. The third point. Abstract classes are not necessarily all abstract methods, abstract and abstract classes can implement some methods. However, the methods in the interface must be public modified, abstract and cannot implement concrete methods.
fourth point. The basic data type in the interface is static and the abstract class is not.


2. Application scenarios

Applications are rule-based applications, that is, grammar-based applications. We can summarize the application scenarios of abstract classes and interfaces according to the similarities and differences in grammar.

There is nothing to say about the same points, we start from different points.


1. The first important difference is that

Abstract classes are not necessarily all abstract methods, but can also have concrete implementation methods, so that the common methods can be promoted to abstract classes, and then concrete methods can be left to subclassesImplement it yourself (the classic application here, the template method design pattern). So abstract classes can better implement code reuse


2. Another important difference is that a class can implement multiple interfaces.

An interface is not the same concept as an abstract class. This can be understood as the interface is the abstraction of the action, the abstract class is the abstraction of the root (that is, the abstraction of the essence is different from the essence of other classes).

An abstract class represents what this object is. The interface represents what the object can do. For example, man, woman, these two classes (if they are classes...), their abstractionsClasses are people. Explain that they are all human. People can eat, dogs can eat, you can define "eat" as an interface, and let these classes implement it.

Therefore, in high-level languages, a class can only inherit one class (abstract class) (just as people cannot be biological and non-biological at the same time), but can implement multiple interfaces (eating interface, walking interface).

When you focus on the essence of a thing, use abstract classes; when you focus on an operation, use interfaces.

另一个重要的概念就是多态,多态通过分离做什么和怎么做,从另一个角度将接口和实现分离出来。多态不但能够改善代码的组织结果和可读性,还能创建可扩展的程序----即无论在项目最初创建时还是在需要添加新功能时都可以“生长”的程序。由于接口更关注于动作的实现,多态主要是分离“做什么”和“怎么做”,所以接口的另一个重要的应用就是多态的实现(当然抽象类也可以实现多态,但是接口更加合适)。


抽象类的功能要远超过接口,但是,定义抽象类的代价高。因为高级语言来说(从实际设计上来说也是)每个类只能继承一个类。在这个类中,你必须继承或编写出其所有子类的所有共性。虽然接口在功能上会弱化许多,但是它只是针对一个动作的描述。而且你可以在一个类中同时实现多个接口。在设计阶段会降低难度的。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324712628&siteId=291194637