Interface Definition

Interface generally refers to an abstraction that an entity provides itself to the outside world (possibly another entity) to separate external communication methods from internal operations so that they can be modified internally without affecting the way other external entities interact with it.

The interface between humans and information machines such as computers or between humans and programs is called the user interface. The interfaces between hardware components of information machines such as computers are called hardware interfaces. The interfaces between software components of information machines such as computers are called software interfaces.

In a computer, an interface is a shared boundary between two independent components of a computer system where information is exchanged. This exchange can occur between computer software, hardware, peripherals, or the person performing the operation, or it can be a combination of them.

​​

Advantages of interfaces

1. Standardization:

Interface is the specification, in the whole system design, involving many layers, in order to make each layer between the call transparent, you only need to know the interface, according to this interface to do your specific things, you can be integrated into the whole system.

Life’s many examples, such as: plugs, sockets, a standard specification tells you plug should be several feet, socket is several holes and so on, plug and socket company is according to the specification for the plug and socket, is driving around the world without the need to finish a plug to try out the plug do right.

2. Scalability

During the development of a project, because the customer’s needs are constantly changing, we have to constantly rewrite existing business code if interfaces are not adopted. Rewriting code may generate new bugs, and rewriting code may affect the classes that call the business, all of which may need to be modified, affecting the stability of the system itself. At the end of the day, your code might be messy and hard to read,

Later, the person who takes over cannot understand the code, and the maintenance work of the system becomes heavier and heavier, which may eventually lead to the failure of the project.

Java interface definition

In the JAVA programming language, an interface is an abstract type, a collection of abstract methods, usually declared as an interface. A class inherits the abstract methods of an interface by inheriting an interface.

Interfaces are not classes. Interfaces are written in much the same way as classes, but they are different concepts. Classes describe properties and methods of objects. Interfaces contain the methods that the class implements.

Unless the class implementing the interface is abstract, that class defines all methods in the interface.

Interfaces cannot be instantiated, but can be implemented. A class that implements an interface must implement all the methods described in the interface, or it must be declared abstract. In addition, in Java, interface types can be used to declare a variable, which can be a null pointer or bound to an object implemented by the interface.

The role of Java interfaces

  • Interfaces allow you to implement the same behavior of unrelated classes without knowing the class to which the object corresponds.
  • Interfaces allow you to specify methods that multiple classes need to implement.
  • Through interfaces, you can understand the interaction interface of an object without knowing its corresponding class.

Class describes an entity, including its state and the actions it may emit.

An interface defines the actions that an entity may emit. But only the prototype of these actions is defined, there is no implementation, and there is no state information.

So an interface is kind of like a specification, a protocol, an abstract concept; A class is a concrete entity that implements the protocol and satisfies the specification. It is a concrete concept.

From a programmatic point of view, interfaces are function declarations and classes are function implementations. It is important to note that there may be multiple implementations of the same declaration.

The interface defines the prototype of a class method, but it cannot be called an empty method, because an empty method means that there is an implementation body, but the implementation body is an empty operation. The interface does not actually define any implementation body. The concrete implementation body is in the class that implements the interface, which only defines how these methods are called.

You can write methods in classes without interfaces, but if you have a set of methods that need to be implemented in many classes, wouldn’t it be better to abstract them into an interface specification?

2. A class describes an entity, which may be a complex object with many actions. If these actions are classified, interface A defines a set of actions, and interface B defines another set of actions, the structure is relatively clear.

This method has the advantages of multiple inheritance and avoids the disadvantages of multiple inheritance. In fact, historically, interfaces have largely been designed to solve the problems of multiple inheritance.

How do you know what methods are defined in the interface defined in the package?

Interfaces define the inputs and outputs of methods. These are protocols, implemented in each class. For many places where you only need an abstract interface, you don’t need to know what the concrete class is, as long as the class implements the interface.

The meaning of Java interfaces

The most important reason for interfaces is the uptransition in Java, where methods that do not exist (or do not share) in a subclass cannot be called. So that’s a bit of a hassle, we need methods in the subclass, but what do we do now?

The first method of adding subclasses to a parent class. However, this method is not really necessary in the parent class, nor is the method function body needed. So we use abstract methods, and that’s how abstract methods are born. But even so, the problem is still not solved, because it is really too troublesome, if there are other subclasses to inherit the parent class, there will inevitably be an upward transition, then, this time, should always add abstract methods to the parent class? Obviously not reasonable!

This led to the second method, the interface, which explains why interfaces are all abstract methods! So with the interface, the method in the subclass needs, but the method in the parent class can be directly used to complete the interface, and there is no need to modify the method of the parent class, and it is more convenient when many people cooperate to complete a project.

Use of Java interfaces

Two similar functions in two classes that call their classes dynamically determine an implementation provide an abstract superclass, and the subclasses implement the methods defined by the superclass.

The problem arises: Java is a single-inheritance language. Generally, which specific class might already have a superclass is solved by adding superclass to its parent class, or superclass to its parent class, until it moves to the top of the class hierarchy. Thus, the design of the insertability of a specific class becomes a modification of all classes in the hierarchy.

1. Pluggability

Any class in a hierarchy can implement an interface that affects all subclasses of that class, but not any superclasses of that class. These will have to realize this method as described in the interface, and its subclasses can automatically from such inheritance, these methods, of course, also can choose from all these methods, or some of these methods, at that time, the subclass has a pluggable (and you can use this interface type load, transfer realized his all subclass).

What we care about is not the specific class, but whether the class implements the interface we need.

The interface provides the intercalability of association and method invocation. The larger the software system is, the longer its life cycle is. The interface ensures the flexibility, expansibility and intercalability of the software system.

Type 2,

Use Java interfaces to couple software units internally and externally. Use Java interfaces that are not concrete classes for variable type declarations, method return type declarations, parameter type declarations, and data type conversions.

Ideally, a concrete Java class should only implement Java interfaces and methods declared in abstract Java classes, and should not give redundant methods.

3. Hierarchical structure

Java interfaces (and abstract classes) are typically used as a starting point for a type’s hierarchical structure.

If a class already has a primary supertype, then by implementing an interface, the class can have another secondary supertype, called a hybrid type.

Java Learning Video:

Java300: The new Java300 is coming! Java zero basic white self learning Java essential quality tutorial

Spend more than 20,000 to buy a full set of Java tutorial, now share to everyone, entry to master! Java300 set _Java program development career tutorial