Abstract classes are templates that are abstracted from multiple classes, on the basis that if this abstraction is made more thorough, it can be refined to a more special
"Abstract class"------interface, the interface cannot contain ordinary methods, all the methods in the interface are abstract methods.
We know that a class is a concrete implementation body, and an interface defines a specification that defines the specifications that a batch of classes must follow, and interfaces that do not care about these
Class, and does not care about the implementation details of the methods in these classes, it only stipulates that certain methods must be provided in the class that provide these methods
will be able to meet the actual needs. It can be seen that the specification is abstracted from multiple similar classes, and the interface does not provide any implementations. The interface embodies the specification
and realize the separation of the design philosophy.
Separating the specification from the implementation is the benefit of the interface, so that the interface-oriented coupling between the components of the software system is a loosely coupled design. Similarly, the software
This interface-oriented coupling should also be used between the modules of the system to minimize coupling between the modules, providing better scalability for the system
and maintainability. Therefore, the interface defines a common code of conduct for multiple classes, which is the channel of communication with the outside, which means that the interface
Typically, you define a common set of methods.
When defining an interface, using the interface keyword, an interface can have multiple direct parent interfaces, but interfaces only inherit interfaces and cannot inherit classes. Common
The interface definition is as follows:
Public interface interface extends interface1,interface2{public static final int count=0;public abstract void Interface0 ( );}
Because the interface defines a specification, the interface cannot contain constructors and initialization block definitions. The interface can contain field (constants only),
Methods (only abstract instance methods), inner classes (including internal interfaces, enumerations) are defined.
As previously known, the interface defines a common code of conduct for multiple classes, so all members of the interface, including constants, methods, inner classes, and pieces
All public access rights. When you define an interface member, you omit the access control modifier, and if you know the access control modifier, you can use only public
Access control modifiers.
For the constants defined in the interface, they are interface-dependent, and they can only be constants, so the system automatically increments these field
Static and final two modifiers. That is, when you define a field in an interface, regardless of whether the public static final modifier is used, the field in the interface is always
These three modifiers will be used to decorate. Furthermore, there are no constructors and initialization blocks in the interface, so the field defined in the interface can only be specified by default when defined
Value.
For methods defined in an interface, they can only be abstract, so the system automatically adds an abstract modifier to it, because the method in the interface
All are abstract methods, so it is not allowed to define static methods in the interface, that is, the method defined in the static modifier interface cannot be used, regardless of the method defined in the interface.
When using the Pulic abstract modifier, the methods in the interface are always decorated with public abstract.
In Java, the class does not support multiple inheritance, in order to compensate for this, Java provides the interface of multiple inheritance, that is, the interface fully supports multiple inheritance, a
An interface can have multiple direct parent interfaces. Similar to class inheritance, a sub-interface extends a parent interface, which obtains all the abstract methods defined in the parent interface, often
Amount When an interface inherits multiple parent interfaces, like the above code, multiple parent interfaces are ranked after the extends keyword, and multiple parent interfaces are separated by commas
Separated.
Similar points about interfaces and abstract classes are as follows:
1. Interfaces and abstract classes cannot be instantiated, they are at the top of the inheritance tree and are implemented and inherited by other classes.
2. Interfaces and abstract classes can contain abstract methods, and ordinary subclasses that implement interfaces or inherit abstract classes must implement these abstract methods.
But the difference between the interface and the abstract class is very large, which is mainly embodied in the design of the two. Interface as a window through which the system interacts with the outside world, then
The mouth embodies a norm. For an interface's implementation, the interface specifies which services the implementation must provide (in the form of a method
For the caller of the interface, the interface prescribes which services the caller can invoke and how to invoke the services (how to
Law).
Abstract class is different, abstract class as a common parent of many sub-classes in the system, it embodies a template-style design. Abstract class as multiple subclasses
Abstract parent class, can be used as intermediate products in the system implementation process, this intermediate product has implemented a part of the system function (has provided the implementation of the party
However, this product is still not considered as the final product and must be further improved.
Therefore, the following differences exist between abstract classes and interfaces:
1, the interface can only contain abstract methods, does not contain the methods that have provided the implementation, abstract classes can completely contain ordinary methods.
2. Static methods cannot be defined in the interface, and static methods can be defined in abstract classes.
3, the interface can only define static constants, can not define ordinary constants; Abstract classes can define either ordinary constants or static constants.
4, the interface does not contain the constructor, the abstract class can contain the constructor, the constructor in the abstract class is not used to create the object, but rather let its subclasses call
These constructors perform initialization operations that are part of an abstract class.
5. The interface cannot contain initialization blocks, but abstract classes can completely contain initialization blocks.
6. A class can have at most one direct parent class, including abstract classes, but a class may implement multiple interfaces directly, by implementing multiple interfaces to compensate for
Lack of Java single inheritance.
Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/43882813 Emotional Control _
Java Object-oriented note 7