编程知识 cdmana.com

Differences between interface and abstract class in Java

1. Definition

abstract class : Use abstract Keyword decorated class .
Interface : Is a reference data type , Is a collection of methods , Use interface Keyword modification .

2. difference

Grammatically :

(1) Member variables in abstract classes can be of various types , The member variables in an interface can only be public static final Type of ( It's a constant );
(2) There is no static code block in the interface , Abstract classes can have static code blocks
(3) A class can only inherit one abstract class , A class can implement multiple interfaces .
JDK8 There can be default methods and static methods , So it's wrong to say that the interface we used to say can't have a way to implement details

On the design level :

(1) An abstract class is an abstraction of something , That is, to abstract the class , And interfaces are abstractions of behavior .( The plane 、 bird )
(2) The design purpose of abstract classes , It's code reuse , It's a template design . The purpose of the interface design is to constrain the behavior of a class ( A constraint ), It's a radial design .
Template design , Abstract class modification , That is to say, make changes to the public part , Subclasses don't have to change ; And radial design , Interface modification , All subclasses that implement the interface have to be modified .

版权声明
本文为[icepopo]所创,转载请带上原文链接,感谢

Scroll to Top