The relationship and difference between Java interface and abstract class.

The relationship and difference between interface and abstract class.

1、 Simple summary

1. Abstract classes and interfaces cannot be instantiated directly. To instantiate, abstract class variables must point to subclass objects that implement all abstract methods, and interface variables must point to class objects that implement all interface methods.

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

3. The interface can only make method declaration, and the abstract class can make method declaration or method implementation

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

5. All abstract methods in an abstract class must be implemented by subclasses. If a subclass cannot fully implement the abstract methods of its parent class, the subclass can only be an abstract class. Similarly, when an interface is implemented, if all interface methods cannot be implemented, the class can only be an abstract class.

6. Abstract methods can only be declared but not implemented. Interfaces are the result of design and abstract classes are the result of reconstruction

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

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

9. Abstract methods should be implemented, so they cannot be static or private.

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

2、 Standard answer

The interface does not require the implementation class and interface to be consistent in concept, but only implements the conventions or capabilities of the interface definition. The interface defines "what to do" and the implementation class is responsible for "how to do", which reflects the principle of separation of function (specification) and implementation. The relationship between interface and implementation can be considered as a "has-a"

Java common classes

Link: https://pan.baidu.com/s/14DPwWZpvwwYXWAMwz3xmnQ Extraction code: lbkx

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>