The difference between abstract class and interface (when to use abstract class and when to use interface?)

When to use

If you have the following situations in your program, then you should consider using abstract classes:

  1. You want to be in several closely relatedSharing code between classes

  2. you wantThose classes that inherit abstract classes have some public methods or propertiesOr has the authority to modify the protected or private attributes. At this time you can also consider using abstract classes.

  3. You want to declare properties that are non-static or non-final. You can obtain and modify the state of the object to which these properties belong.

When there are the following situations, we can consider using interfaces:

  1. Implement your interface with unrelated classes. For example, comparable and cloneable interfaces can be implemented using unrelated classes.

  2. You want to specify oneBehavior of specific data types, But don’t care about who will implement this behavior.

  3. You want to take advantageAdvantages of multiple inheritance

the difference

  • An abstract class is an abstraction of a class (What is it), the interface is an abstraction of behavior (doing what)。

  • If the behavior spans objects of different classes, you can use interfaces. For some realistic class objects, use inherited abstract classes.

  • The abstract class discovers the common things from the subclass, generalizes the parent class, and then the subclass inherits the parent class, and the interface does not know the existence of the subclass at all, and how to implement the method has not been confirmed and defined in advance.

  • The use of abstract classes and interfaces is the difference between is and has. If it is a certain kind of thing, for example, a person is relative to a man, a child, or a woman, then it should be an abstract type, because they are all human beings with all the characteristics of human beings. Actionable relative to people, cars and airplanes are hass, so they are interfaces, because they have such functions but do not have the characteristics of is.

  • Third speech

The biggest difference between abstract classes and interfaces is that the properties of abstract classes can be inherited.
The interface has only constants.
In general, we use interfaces as much as possible, because interfaces describe object characteristics from methods and can implement multiple interfaces, but inheritance can only inherit one.
So when do we want to call the abstract class?
That's because the subclass must contain a certain characteristic attribute of the parent class.
For example, people and fish, and both people and fish need to breathe oxygen for survival. This breathing is a method, so an interface is needed.
When you need to distinguish between people breathing with their lungs in the air and fish breathing with their gills in the water, then abstract types are used.

Why can only constants be defined in interfaces?

After learning the interface, I know that only constants can be defined in the interface. Even if your code is written like this: public int a, the underlying language will add public static final int a to constants by default, so why?

We all know that the implementation of methods cannot be defined in interfaces, but the implementation of variables, constants and methods can be defined in abstract classes, so we can regard interfaces as a higher level of abstraction than abstract classes.Special abstract class. Adding the interface can define variables, because the methods in the interface are abstract, weIt is not possible to modify this attribute value through behaviors, such as the set() method

Perhaps some people will say that we can let the object of the class that implements the interface modify the attribute value of the interface. If this is the case, for example, in the interface, I define a variable a, and we change a through the object of the class B that implements this interface. Value, then other variables a in class B and class C that implement this interface will be known to change (because they all implement the same interface and deal with the same variable, and the variable processing method is by address ), then it will causeThe class that wants to implement this interface does not know what the value of the variable a in the interface is, which will cause confusion

Moreover, the abstract concept is to extract the immutable things and encapsulate them together, and put the mutable things into the realization. Since the design concept of the interface is a high-level abstraction, it should be defined as immutable things. If the interface Variables defined in, it means that it has a variable component, which is not a high-level abstraction.

Summary: Interface is a high-level abstraction, which embodies the design principle of OCP (closed for modification and open for extension), which is what we have been pursuing in our design.

Guess you like

Origin blog.csdn.net/weixin_46168350/article/details/111060350