The difference between JAVA syntax abstract class and interface

Preface

At first glance, the functions implemented by the two seem to be similar, because neither can be instantiated, but in fact the difference is still very big


the difference

  1. Abstract classes must be inherited
    by subclasses, interfaces must be implemented by classes
    Insert picture description here

  2. Interface can only do method declarations
    abstract class can be used as a method declaration , you can do way to achieve
    Insert picture description here

  3. Variables defined in interfaces can only be public static constants
    Variables in abstract classes are ordinary variables
    Insert picture description here

  4. Interfaces are the result of design.
    Abstract classes are the result of refactoring .

    To put it in a bit easier to understand here, it is:
    interface , which is to write all the subtitles before writing the article. Then everyone uses the same subtitles, but the following articles can be free to use the
    abstract category , that is, write all the subtitles before writing the article, but you can also write some articles in advance under the subtitles. Then when others use these titles, they can use those articles directly, or they can rewrite their own articles instead of those articles.
    Insert picture description here

  5. Abstract classes and interfaces are used to abstract concrete objects, but the abstraction level of the interface is the highest
    Insert picture description here

  6. Abstract classes can have concrete methods and attributes.
    Interfaces can only have abstract methods and immutable constants.
    Insert picture description here
    7. Abstract classes are mainly used to abstract categories. The
    interface is mainly used to abstract functions.
    This shows the essence! ! !
    Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44062380/article/details/107008940