Interface and type safety of Java language


Interface is the key to component pluggability. The key to pluggable components is that there is a common interface and each component implements this interface.

 What is an interface?

An interface in Java is a declaration of a series of methods and a collection of method features. An interface has only method features and no method implementation. Therefore, these methods can be implemented by different classes in different places, and these implementations can have different behaviors (functions).

Two meanings of interface: first, java interface, the structure existing in Java language, with specific syntax and structure; 2、 The feature set of methods of a class is a logical abstraction. The former is called “java interface” and the latter is called “interface”.

In the Java language specification, the characteristics of a method only include the name of the method, the number and type of parameters, but not the return type of the method, the name of the parameters and the thrown exceptions. When the java compiler checks method overloading, it will judge whether the two methods are overloaded methods according to these conditions. However, when the java compiler checks the substitution of methods, it will further check whether the return types and exceptions thrown by the two methods (separating supertypes and subtypes) are the same.

The rules of interface inheritance and implementation inheritance are different. A class has only one direct parent class, but it can implement multiple interfaces.

The java interface itself does not have any implementation, because the java interface does not involve representation, but only describes public behavior, so the java interface is more abstract than the Java abstract class.

Java interface methods can only be abstract and public. Java interfaces cannot have constructors. Java interfaces can have public, static and final attributes.

The interface separates the characteristics of the method from the implementation of the method. This segmentation is reflected in that the interface often represents a role, which packages the operations and properties related to the role, and the class implementing the interface is the actor playing the role. A role is played by different actors, and different actors do not require anything in common except playing a common role.

 Why use interfaces?

Two similar functions in the two classes call their classes to dynamically determine an implementation. They provide an abstract parent class, and the child classes implement the methods defined by the parent class respectively.

Problem: Java is a single inheritance language. Generally, which specific class may already have a superclass. The solution is to add a parent to its parent class or add a parent to its parent class, and only move to the top of the class hierarchy. In this way, the design of the pluggability of a specific class becomes the modification of all classes in the whole hierarchy.

Interface is the guarantee of pluggability.

Any class in a hierarchy can implement an interface, which will affect all subclasses of this class, but will not affect any superclasses of this class. This class will have to implement the methods specified by this interface, and its subclasses can automatically inherit these methods from this class. Of course, they can choose to replace all these methods or some of them. At this time, these subclasses have pluggability (and can be loaded with this interface type and pass all its subclasses).

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

The interface provides the pluggability of association and method call. The larger the scale of the software system, the longer the life cycle. The interface ensures the flexibility, scalability and pluggability of the software system.


Use Java interfaces to couple software units internally and externally. The java interface is not a specific class for variable type declaration, method return type declaration, parameter type declaration, and data type conversion.

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

  Type hierarchy

Java interfaces (and abstract classes) are generally used as a starting point for the hierarchical structure of a type.

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

 Common methods of java interface

Single method interface

  public interface Actionlistener(){

  public abstract void actionPerformed(ActionEvent event);


There is only one method. Only by implementing this interface (overriding the only method in this interface), can you register in the event listener list (the parameter is of actionlistener type). When the event source changes, this unique actionperformed method will be called automatically

Identification interface

Is an interface without any methods and properties. The identification interface does not have any semantic requirements for the class implementing it. It only indicates that the class implementing it belongs to a specific type (passing).

Excessive use of identity interfaces is not recommended.

Constant interface

Use the java interface to declare some constants, and then use them by the class that implements the interface (I’ve done this before when making a Sketchpad). It is not recommended to imitate this constant interface.

  Java language type security issues

Java is a strongly typed language. This means that the java compiler will check the code to make sure that each method call conforms to the type without any assignment. If there is any inconsistency, the java compiler will give an error.

Type checking is based on the simple fact that each variable declaration gives the variable a type; Each method, including the declaration of the constructor, gives the characteristics of the method. In this way, the java compiler can infer an obvious type from any expression, and the java compiler can check the type based on the obvious type.

The Java language is type safe. That is, any legitimate Java class accepted by the java compiler is guaranteed to be type safe. In other words, there will be no errors of any kind during the running of the program. It is impossible for a java program to treat a variable that originally belongs to one type as another type, so it will not produce errors caused by it.

In short, the Java language relies on three mechanisms to achieve type safety: type checking during compilation, automatic storage management, and array boundary checking.

Note: most of the content of this article comes from teacher Yan Hong’s Java and patterns.

Recommended Today

The best Feynman learning method in history, maybe you have always understood it wrong!

When it comes to Feynman learning method, I think you must feel very familiar with it? Feynman learning method, the ultimate learning method, is widely spread on the Internet. How to understand? And many authors speak very fully from the three aspects of what, why and how. What is Feynman learning method? It is very […]