Java Design pattern (designer Patterns in Java) reading abstract 1th Interface Mode--2nd Chapter introduction of interface Mode

Source: Internet
Author: User

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
  1. 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.
  2. Implementation classes can provide empty implementations (stubs), such as WindowListener and Windowadapter
  3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.