`Differences between ‘abstract class’ and’ interface ‘in Java

Time:2020-1-16

abstract class

JavaAllowed inabstractThe modifier declares the method. At this time, only the method is defined but not implemented(abstractThe decorated method has no body, only a signature and a semicolon
Below isabstractMethods and theabstractClass related rules:

  • As long as there is one in the classabstractMethod, the class itself automatically becomesabstract, and must be declared asabstract classOtherwise, compilation errors will be caused;
  • abstract classCannot instantiate;
  • abstractSubclass of a class must cover every subclass of a superclassabstractMethods and implement them all (that is, provide the method body) to instantiate them. This kind of class is generally called concrete subclass(concrete subclass), to emphasize that it is not an abstract class
  • Ifabstract classThe subclass of does not implement all of the integratedabstractMethod, then this subclass is stillabstract class, and must useabstractDeclaration;
  • Usestatic,privateas well asfinalDeclared methods cannot be abstract methods, because these three methods cannot be overridden in subclasses,final classThere cannot be anyabstractMethod;
  • Even if there is noabstractMethod, this class can also be declared asabstract. declared using this methodabstractClass indicates that the class implementation is not perfect and needs to be handed over to the subclass implementation. This class cannot be instantiated

interface

JavaClass inheritance of is a single inheritance mode. For further object-oriented extension,JavaCreatedinterfaceConcept.
interfaceIt’s just a descriptionAPITherefore,interfaceProvides a description of the type and implements thisAPIThe method that should be provided by the

Below isinterfaceRelevant rules:

  • interfaceAll mandatory methods in are implicitly usedabstractDeclaration, cannot have method body, use semicolon (can useabstractModifier, but generally omitted);
  • Interface definition exposedAPI.interfaceAll members in are implicitly usedpublicDeclaration, and used to omit unnecessarypublicModifier. If used in an interfaceprotectedperhapsprivateDefining methods will lead to compilation errors;
  • interfaceNo potential instance field can be defined in. Field is implementation detail, while interface is rule or not in advance. In interface, you can only define and use at the same timestaticandfinalDeclared constant;
  • The interface cannot be instantiated, so the constructor cannot be defined;
  • An interface may contain nested types. Nested types implicitly usepublicandstaticDeclaration;
  • fromJava 8At first,interfaceStatic methods can be included in

Difference summary

abstract classAndinterfaceThere are many connections and differences
The following is a summary of individual differences in spending:

  • abstract classyesclassThere are unimplemented methods in, which are left to be implemented by the inherited subclass,interfaceThe methods in are all unimplemented(Java 8ininterfaceMediumdefaultMethod excluded);
public interface List extends Collection {
    //Methods decorated with 'default' in 'interface', providing default implementation
    default void sort(Comparator super E> c) {
        Object[] a = this.toArray();
        Arrays.sort(a, (Comparator) c);
        ListIterator i = this.listIterator();
        for (Object e : a) {
            i.next();
            i.set((E) e);
        }
    }
}
  • Subclass can onlyextendssingleabstract class, but you canimplementsMultipleinterface;
  • abstract classMedium energy declarationnon finalVariables (such asint test;),interfaceCan only be declared bystatic finalModified variables;
  • abstract classThe middle variable can beprivate,publicas well asprotectedModification,interfaceMedium variable can only bepublicModification;
  • intercaeSureextendsMultipleinterfaceAndabstract classSureextendsA class (not limited toabstract class)And multipleinterface.

Coding usage

In the daily coding work, selectabstract classperhapsinterfaceCaution is needed

  • abstract classIt indicates the inheritance relationship, which is essentially “is a” relationship;
  • interfaceIt means there is a certain behavioral relationship, yeslike aRelationship.

withDoor with alarmFor example, she has the function of a door and an alarm function

  • Inheritance door (door isabstract class, subclass to implement switch function);
  • Realize alarm function (realize alarminterface, complete the alarm function)

PS:
If you think my article is helpful to you, you can scan the code to get the red packet or scan the code support (any amount, a penny is love), thank you!

Alipay red envelope Alipay WeChat
`Differences between 'abstract class' and' interface 'in Java `Differences between 'abstract class' and' interface 'in Java `Differences between 'abstract class' and' interface 'in Java

Recommended Today

[Redis5 source code learning] analysis of the randomkey part of redis command

baiyan Command syntax Command meaning: randomly return a key from the currently selected databaseCommand format: RANDOMKEY Command actual combat: 127.0.0.1:6379> keys * 1) “kkk” 2) “key1” 127.0.0.1:6379> randomkey “key1” 127.0.0.1:6379> randomkey “kkk” Return value: random key; nil if database is empty Source code analysis Main process The processing function corresponding to the keys command is […]