Reprint Please specify the Source:Jiq ' s technical Blog
First, Introduction
Based on the following two principles in the object-oriented five principles, we should consider using interfaces and abstract classes more than one:
Richter Substitution principle: Subclasses can replace the parent class by implementing the parent class interface, so the parent class should be implemented as an interface/abstract class as much as possible. Facilitates the expansion of programs without affecting existing code.
dependency Inversion principle: Unlike structured design, we want to make the bottom layer dependent on the upper layer. And not the other way around.
While designing interfaces and abstract classes, we should follow the principle of "interface isolation" in the five principles of object-oriented. Even with a dedicated interface than using a single interface good. This guideline corresponds to the "single responsibility Principle" for class design.
Note: There are very many patterns in design patterns that are related to interfaces and abstract classes. For example, abstract factories, template methods, and so on.
Second, the concept
Abstract class (AbstractClass)
(1) abstract classes provide a common structure and prevent users from instantiating them;
(2) Although an abstract class cannot be instantiated, it still has constructors.
------ why???
(3) assuming you inherit from an abstract class, you have only two choices: implement all the abstract methods in the base class, and declare yourself an abstract class.
(4) Let's say you want to make all of the exported classes replicate or implement a method. Then you can declare it as abstract to force the export class to implement it.
(5) Java does not agree with the multiple inheritance of a class. However, multiple interfaces can be implemented. interfaces can inherit multiple.
Interface (Interface)
(1) interfaces and internal classes provide us with a more structured approach to separating interfaces from implementations.
(2) an interface is a completely abstract class that does not provide any detailed implementation;
(3)Interface Variables: Implied is Public. Staticand theFinalof,is a total constant that cannot be changed after initialization. There is only one copy in memory.
(4) interface method: The default is public Whereas the normal class method ( contains ) Span lang= "ZH-CN" style= "font-family: Arial" > The constructor defaults to the (protected)
( 5) interface itself : Default is package access protected Span lang= "ZH-CN" style= "font-family: Song Body" and ( inheritance interview Permissions ) but can only be used Public & Abstract to decorate an interface.
(6) Multiple Inheritance: not the same as the class. Interfaces agree to multiple inheritance . An interface can inherit multiple interfaces at the same time.
(7) the interface itself can only be declared as public& Abstract and. Other modifiers (private, Protected, static,final, transient& volatile) are not agreeable.
Java Basics: Abstract classes and interfaces