The interface of a class is the method and character set that the class allows other class objects to access. Interfaces are a promise that other interfaces will follow. The implementation of the class-to-interface is the code in the implementation class method.
Java allows a class to implement multiple interfaces, and an interface is implemented by multiple classes.
The adapter (Adapter) mode satisfies the customer's needs by using an interface type to adapt the interface of the class.
Interfaces and abstract classes
Challenge 2.1
Write a three-point distinction between abstract classes and interfaces in Java.
• A class can implement multiple interfaces, but can inherit at most one abstract class
• Abstract classes can contain specific methods, and all methods of an interface are abstract
• Abstract classes can declare and use fields; interfaces do not, but you can create static final constants
• The method of an abstract class can be public, protected, private, or default package;
• Abstract classes can define constructors; interfaces cannot
Interfaces and responsibilities
- Interfaces restrict collaboration between objects, which in fact provides greater freedom. It actually prescribes a protocol in which classes that implement this interface must adhere to this protocol. Then no matter how the implementation class changes, as long as it complies with this protocol, then the interface does not need to change.
- Implementation classes can provide empty implementations (stubs), such as WindowListener and Windowadapter
- Interfaces can be used to declare constants
Summary
The power of an interface is that it describes the behavior that it expects and does not expect in class collaboration. An interface is similar to an abstract class, but it defines the behavior without providing a concrete implementation.
Beyond the Normal interface
Java Design pattern (designer Patterns in Java) reading abstract 1th Interface Mode--2nd Chapter introduction of interface Mode