The difference between abstract and interface of the java

1. same point
both A. are abstract classes are not instantiated.
B. interface implementation class and subclass abstrctclass have already declared abstract method must be achieved.

 

2. Different points

A. interface need to implement, use implements, and abstract class needs to inherit, use extends.
B. A class can implement multiple interface, but a class can only inherit an abstract class.
C. interface emphasized achieve a particular functionality, and abstractclass emphasize affiliations.
Although the interface implementation class D. abstrct class and subclass must implement the corresponding abstract methods, but of different forms. Each of the interface methods are abstract, just a declaration (declaration, there is no method body), the implementation class must implement. Abstractclass and subclass can be selectively implemented.

This choice has two meanings: 
    First, Abastract class not all the methods are abstract, and only those methods have abstract of the crown is abstract, a subclass must implement. No abstract of those methods must be defined in the method body in Abstrct class. 
    Second, abstract subclass of class during inherit it, either direct inheritance of non-abstract method can also be covered; and for abstract methods, you can choose to implement, can also restated its approach to abstract way, without the need to achieve, to stay to its subclasses to achieve, but these must be declared as an abstract class. Both abstract class, of course, can not be instantiated. 
E. abstract class is the interface to the Class intermediary. 
The method interface is completely abstract, the method can only be declared, and can only be declared pulic, the method can not be declared private and protected, and can not define method body, we can not declare instance variables. However, interface but can declare a constant variable and difficult to find such examples in the JDK. But the constant variable in interface in violation of its role as an interface exists purposes, but also confused the different values of the class interface. Indeed, if required, can be placed in the respective abstractclass or Class. 
abstract class played a role in connecting the interface and the Class. In one aspect, abstract class is abstract, can declare abstract methods to standardize subclasses must implement the functions; on the other hand, it can define a default method bodies, for direct use or subclasses covered. In addition, you can also define your own instance variables for subclasses to use through inheritance.

 

 

Excerpt: https://blog.csdn.net/Mandypan/article/details/52138062

Guess you like

Origin www.cnblogs.com/peifengyang/p/11411022.html