【Interview】The difference between abstract class and interface

Interface (English: Interface), in the JAVA programming language, is an abstract type and a collection of abstract methods. Interfaces are usually declared as interfaces. A class inherits the abstract methods of the interface by inheriting the interface.

Abstract class (English: abstract class), in the object-oriented concept, all objects are described by classes, but in turn, not all classes are used to describe objects, if a class does not contain Enough information to describe a concrete object, such a class is an abstract class. Except that the abstract class cannot instantiate the object, other functions of the class still exist, and the access methods of member variables, member methods and constructors are the same as those of ordinary classes. Since abstract classes cannot instantiate objects, abstract classes must be inherited in order to be used. It is also for this reason that the decision to design abstract classes is usually made during the design phase.

An interface is not the same concept as an abstract class. An interface is an abstraction of actions, an abstract class is an abstraction of roots.

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 abstract class is person. Explain that they are all human. People can eat, dogs can also eat, you can define "eat" as an interface, and then 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).

The difference is as follows:

1. Neither abstract classes nor interfaces can be directly instantiated. If they are to be instantiated, the abstract class variable must point to the subclass object that implements all abstract methods, and the interface variable must point to the class object that implements all interface methods.

2. Abstract classes should be inherited by subclasses, and interfaces should be implemented by classes.

3. The variables defined in the interface can only be public static constants, and the variables in the abstract class are ordinary variables.

4. The abstract methods in the abstract class must all be implemented by the subclass. If the subclass cannot implement all the abstract methods of the parent class, then the subclass can only be an abstract class. Similarly, when an interface is implemented, if it cannot implement all the interface methods, then the class can only be an abstract class.

5. Abstract methods can only be declared, not implemented. Interfaces are the result of design, and abstract classes are the result of refactoring.

6. There can be no abstract methods in an abstract class

7. If a class has abstract methods, then the class can only be abstract

8. Abstract methods must be implemented, so they cannot be static or private.

9. Interfaces can inherit interfaces and can inherit multiple interfaces, but classes can only inherit from a single root.

-------------------------------------------------------------------------------------------------------------

When focusing on the essence of a thing, use an abstract class; when focusing on an operation, use an interface.

Abstract classes are much more functional than interfaces, but defining abstract classes is expensive. Because in high-level languages ​​(and in terms of 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 be much weaker in function, it is only a description of an action. And you can implement multiple interfaces in one class at the same time. Difficulty will be reduced during the design phase.

References:

http://www.runoob.com/java/java-interfaces.html

http://www.runoob.com/java/java-abstraction.html

https://www.cnblogs.com/yongjiapei/p/5494894.html

Guess you like

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