Java object-oriented interface – interface

What is an interface

Interfaces in general computers are divided into hardware interfaces and software interfaces.

  • Hardware interface: refers to the connection mode between two hardware devices, including physical interface and logical data transfer protocol.

  • Software interface: usually refers to the program code, in Java represents a special class, is a characteristic set of N methods, represents a specification; There is also the case of a data interface, which represents the way data is interacted between a client and a server.

For example, the display package a lot of electronic components, these components are connected together to provide support for the normal work of the display, so how to display and the outside of the other equipment (such as computer host, audio, headphones and other equipment) communication interaction? Also through the display exposed communication interface – exposed “holes” in the display. By connecting these “holes”, other devices can work with the display, capture information from the display and provide it with features that the display does not, such as excellent acoustics.

Interface design purpose: From the above display example, it is easy to see that the interface can be regarded as a communication bridge between devices, through the exchange of data to complete the communication. An interface represents a specification that a specification/constraint/implementer must follow in order to constrain what a consumer should do. So without these specifications (interfaces), what are the problems?

A mobile phone charger is an excellent example of interface in action. In today’s era of smartphone flooding, I don’t know if we miss the era of functional machines and shanzhai machines. In those days, charging devices between mobile phones had their own “features” and were different from one brand to another.

If the charging device is lost or damaged, it often costs a lot to buy a new one, so universal chargers were popular at the time, but their charging efficiency was not satisfactory. The problems brought by this greatly troubled the majority of mobile phone users.

It wasn’t until the advent of The Android system that the charging interface specification was unified that this problem was effectively resolved. Since Android unified the specification of mobile phone charging interface, almost all Android phones have the same charging interface (micro-USB/Type-C /Lightning). Of course, the iPhone is a maverick and is not part of the discussion.

The USB interface

In life, USB interface and we often say USB slot is actually a deviation, it is not what we usually see those USB slots, but those USB slots follow a specification (standard). USB is the Universal Serial Bus, used to standardize the connection and communication between computers and external devices, widely used in the FIELD of PC interface technology.

The USB slots we see are designed according to the USB interface specification. For different TYPES of USB devices, their respective USB slots need to follow the same specification, so that any device inserted into the USB slot can communicate with the motherboard.

For multiple USB slots on the same model of motherboard, they have the same data exchange method, the same implementation details; In object-oriented thinking, they can be considered different instances of the same class, which means that USB slots are instances of the USB specification.

Interfaces only define the specifications that should be followed, but do not care about the internal data of these specifications and the implementation details of their functions, thus separating the specifications from the implementation and enhancing the scalability and maintainability of the system. The advantages of this: for example, the motherboard provides a USB slot, as long as a mouse that follows the USB specification, you can plug into the USB slot and communicate with the motherboard normally, regardless of the manufacturer of the mouse, the internal implementation of the mouse. When the mouse is broken, just change the mouse.

Interfaces in Java

Interfaces in Java are written down as abstractions of multiple abstract classes. In layman’s terms, the smallest program unit in Java is a class, and an interface is actually a special class. Like USB interfaces, interfaces in Java represent specifications that define a set of abstract methods that represent the functions that a class of things must have, and an implementation class for the interface that implements the interface and provides the implementation of each method in the interface.

  • Define class syntax: Permission modifier class name {}

  • Define interface syntax: permission modifier interface name {}

Interface naming:

  1. Capable of some kind. Can end with able/handler. Such as: Walkable. Java

  2. The interface starts with I, for example, iWalkable.java

Interfaces, like classes, are bytecode files of a. Class when compiled.

Members in the interface:

1. There is no constructor in the interface because the interface cannot use the new keyword to create objects (it cannot be new);

2. Common methods cannot be defined in the interface. Methods in the interface are public abstract methods by default. Therefore, the public and abstract modifiers for methods in an interface can be omitted.

Member variables defined in the interface are global static constants. By default, public static final is used to modify member variables.

  • Public static final String NAME = "static final String NAME ";

4. Inner classes defined in the interface are public static inner classes. By default, we use public static to refer to inner classes.

  • public static interface Openable {}

Flag interface: An interface that provides only one interface definition without any members is just a flag. If there is a class that implements the interface, that class belongs to the interface faction. This is not used much, just understand.

Constant interface: The use of an interface to encapsulate multiple constant information, called a constant interface, serves the same purpose as a regular constant class, but is not recommended here.

Interface features:

  1. Interfaces have no constructors and cannot explicitly define constructors, so they cannot be instantiated.

  2. The methods and methods in the interface are all abstract public, and the default modifier is public abstract.

  3. All fields in the interface are global static constants. The default modifier is public static final.

  4. All internal classes in an interface are public static. The default modifier is public static.

Interface inheritance:

Interfaces can only inherit from interfaces, not from classes, and interfaces support multiple inheritance (classes are single inheritance). The interface inheritance syntax is as follows:

  • [Modifiers] interface Interface name extends interface 1, interface 2

Inheritance relationships between classes are represented by the extends keyword. Interfaces can only be inherited relationships between interfaces, which is also represented by the extends keyword.

An interface can only have an implementation relationship with an implementation class, and it is a class-implementation interface, represented by the implements keyword.

Interface implementation

Interfaces simply define what a class of things should do, but they don’t provide any implementation. But in Java 8 and later, interfaces can be implemented by default. At this point, we define a class, and then use the class to implement the interface, so we must override the methods in the interface, provide function implementation, so as to achieve the function defined in the class interface.

Class implementation interface: A class can implement multiple interfaces, thus eliminating the problem of single inheritance of classes. The syntax is as follows:

  • Extends implements interface 1, interface 2{}

The relationship between an interface and an implementation class is called an “implementation relationship” and is represented by the implements keyword, but is sometimes called a special inheritance relationship for convenience. An interface is the parent of an implementation class: an implementation class is a subclass of an interface.

Interface oriented programming

The polymorphic relationship between interfaces and implementation classes is a common manifestation of polymorphism in development

  • Interface variable = create implementation class object; // Show the polymorphism idea

The methods in the interface are public abstractions, so the implementation class must override the methods in the interface, and the methods must use public decorations, because methods in the interface use public decorations by default.

Similarities between interfaces and abstract classes:

  1. Are at the top of inheritance, used to be implemented or inherited by other classes or interfaces;

  2. Can’t be instantiated;

  3. Can define abstract methods that must be overridden by subclasses/implementation classes;

Differences between interfaces and abstract classes:

  1. Interfaces have no constructors; abstract classes have constructors.

  2. Abstract classes can contain both ordinary and abstract methods, and interfaces can contain only abstract methods (prior to Java 8);

  3. Classes are single-inherited. A class can inherit only one direct parent (possibly an abstract class). Interfaces support multiple inheritance. A class can implement multiple interfaces (interfaces compensate for Java’s single inheritance);

  4. Member variables: The default is public static final, abstract class is the default package permissions;

  5. Methods: The interface defaults to public abstract, and the abstract class defaults to package access.

  6. Internal classes: the default is public static for interfaces, and the default is package access for abstract classes.

If an interface and an abstract class can do the same thing, try to use the interface. This is interface oriented programming. Interface oriented programming is to use the benefits of polymorphism, the implementation class object assigned to interface type variables, shielding implementation differences between different implementation classes, so as to achieve universal programming.

Case in point: Using a USB device to work.

The end. Although the old man is not serious, but the old man’s talent