`Java` in` abstract class` difference with `interface`

abstract class

JavaAre allowed abstractmodifiers method declaration, but this time not only define the method implementation method ( abstractmodified method has no body, only a signature and a semicolon).
The following is the abstractmethod and location of these methods abstractassociated with the class rules:

  • As long as there is a class abstractmethod, then the class itself will automatically become abstract, and must be declared abstract class, otherwise it will cause a compiler error;
  • abstract classCan not be instantiated;
  • abstractSubclasses must override each super class class abstractmethod, and achieve all these methods (i.e., methods providing the body), can be instantiated. This class generally known as concrete subclasses ( concrete subclass), in order to stress that it is not an abstract class.
  • If a abstract classchild class does not implement all the integration abstractmethod, then this sub-category is still abstract class, and must use the abstractdeclaration;
  • Use static, privateand finaldeclared method is not an abstract method, because the three methods can not be overridden in a subclass like. final classCan not be any abstractmethod;
  • Even if not a class abstractmethod, the class can be declared abstract. Using this method declaration abstractclass indicates that the class implements imperfect, we need to subclass implementation of this class can not be instantiated.

interface

JavaSingle inheritance class inheritance pattern, in order to further extend the object-oriented, Javacreating a interfaceconcept
interfaceis the role of drawing API, therefore, interfaceto provide a description of the type of information, and the realization of this APImethod should provide a class.

The following are the interfacerelevant rules:

  • interfaceAll methods implicitly use forced abstractstatement, the body can not have a method to use the semicolons (use abstractmodifier, but generally will be omitted);
  • Public interface definition API. interfaceAll members are implicitly using publicstatement, and omitting unnecessary habit publicmodifier if used in the interface. protectedAlternatively privatedefined method causes a compilation error;
  • interfaceCan not define any potential instance fields fields are implementation details, and the interface is not a rule in advance, in the interface can only be defined using both. staticAnd finalconstant declarations;
  • Interfaces can not be instantiated, the constructor and therefore can not be defined;
  • Interface type may contain nested nested type used implicitly. publicAnd staticdeclarations;
  • From the Java 8beginning, interfaceit can contain static methods.

Difference summary

abstract classAnd interfacethere are many links, there are also differences.
The following is a summary of the differences in personal expenditure:

  • abstract classA classmethod is not implemented in the present, it left inherited subclass implementation, interfacethe method is not implemented in the method are ( Java 8in interfacethe defaultexcluding method);
public interface List<E> extends Collection<E> {
    // `interface`中被`default`修饰的方法,提供默认实现
    default void sort(Comparator<? super E> c) {
        Object[] a = this.toArray();
        Arrays.sort(a, (Comparator) c);
        ListIterator<E> i = this.listIterator();
        for (Object e : a) {
            i.next();
            i.set((E) e);
        }
    }
}
  • Subclass only extendsa single abstract class, but implementsa plurality interface;
  • abstract classIt can be declared non finalvariable (such as int test;), interfacethe declaration is only static finalmodified variable;
  • abstract classIt may be variable private, publicas well as protectedmodified interfacevariable only be publicmodified;
  • intercaeIt can be extendsmultiple interface, and abstract classcan be extendsa class (non-limited abstract class) and a plurality interface.

Use coding

In the daily work of coding, choose abstract classor interfaceneed to be careful.

  • abstract classRepresent inheritance, an inheritance relationship is "is a" relationship in nature;
  • interfaceIt represents a certain behavior relationship is like athe relationship.

With 带报警器的门, for example, she has the role of the door, also has an alarm function.

  • Inherited door (the door for abstract classsubclasses implement switching function);
  • Implement alarm function (implemented alarm interface, the alarm function is complete).

PS:
If you think my article helpful to you, you can scan the code to receive support under a red envelope or scan code (random number, a penny is love), thank you!

Alipay red envelope Alipay Micro letter

Guess you like

Origin www.cnblogs.com/jason1990/p/11606995.html