The common characteristics of interfaces and abstract classes are as follows:
Neither the interface nor the abstract class can be instantiated, at the top of the inheritance tree, for implementation and inheritance by other classes.
Both interfaces and abstract classes can contain abstract methods, classes that implement interfaces, or classes that inherit abstract classes must implement these abstract methods.
difference: The main difference between the two is that they exist for different purposes, that is, the purpose of the design is different.
Interface as a window of interaction between the system and the outside world, the interface embodies a specification.
Abstract class, as the common parent of many subclasses in the system, is a kind of template design.
What we are most concerned about is the difference between the two usages:
(1) An interface can contain only abstract methods and default methods, and cannot implement common methods. Abstract classes can completely contain common methods.
The default method is as follows:
Public interface A { default void foo () { System.out.println ("Calling A.foo ()");} } public class Clazz implements A {} Even if the Clazz class does not implement the Foo () method. The default implementation of the Foo () method is provided in interface A. This is the default method .
(2) The static method cannot be defined in the interface, and can be in an abstract class.
(3) Only static variables can be defined in an interface, and ordinary variables cannot be defined, and abstract classes can define static variables and can define ordinary variables.
(4) The interface does not contain a constructor, and an abstract class can contain a constructor. But instead of creating the object, let the child class call the constructor to complete the initialization of the abstract class.
(5) The interface cannot contain an initialization block, but an abstract class can have initialization blocks.
(6) class is a single inheritance, including abstract classes, but the implementation of the interface can be multiple, supplementing the Java single inheritance of the shortage!
The difference between an interface and an abstract class in Java