java foundation: classes and interfaces

Abstract classes and interfaces contrast
abstract class is used to capture characteristics common subclass. Interface is a set of abstract methods.

From the design perspective, the abstract class is an abstract class is a template design, the interface is abstract behavior, is a code of behavior.

Same point

Abstract classes and interfaces can not be instantiated
are located in succession to the top, the other for being implemented or inherited
contains abstract methods that subclasses must override these methods abstract
differences

Note : Java8 the default interface methods and static methods introduced in order to reduce the difference between abstract classes and interfaces.

Now, we can provide a method for the default implementation of the interface, and without forcing subclasses to implement it.

Interfaces and abstract classes have advantages and disadvantages, the choice of interfaces and abstract classes must comply with such a principle:

Behavioral models should always through the interface instead of an abstract class definition, it is usually preferred interface to minimize the use of abstract classes.
Select the abstract class when usually be the case: the need to define a subclass of behavior, but also provides common functionality into sub-categories.

 

Published 438 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_37769323/article/details/104602776