Interfaces are the key to implementing component insertability. The key to insertable component is the existence of a common interface and each component implements this interface.
What is an interface?
Interfaces in Java are declarations of a series of methods, and are a collection of method features. An interface only has the characteristics of methods but no method implementation. Therefore, these methods can be implemented by different classes in different places, and these implementations can have different behaviors (functions).
There are two meanings of interfaces: one, Java interface, the structures that exist in the Java language have specific syntax and structure; two, the feature set of methods that a class has 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, and do not include the return type of the method, the name of the parameter, and the thrown exception. When the Java compiler checks the overload of a method, it will determine whether the two methods are overloaded methods based on these conditions. However, when the Java compiler checks for the permutation of the method, it will further check whether the return type and thrown exception of the two methods (subtypes and subtypes) are the same.
The rules for interface inheritance and implementation of 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 appearances, but only describes public behavior, so the Java interface is more abstract than the Java abstract class.
The methods of Java interface can only be abstract and public. The Java interface cannot have a constructor. The Java interface can have public, static and final properties.
The interface separates the characteristics of the method and the implementation of the method. This segmentation is reflected in that the interface often represents a role, which wraps the operations and attributes related to the role, and the class that implements the interface is the actor who plays the role. A role is played by different actors, and different actors do not require any other commonalities except playing a common role.
Why use interface?
Two similar functions in the two classes call their class dynamically to determine an implementation, and they provide an abstract parent class, and the child class implements the methods defined by the parent class respectively.
Problem: Java is a single inheritance language. Generally speaking, which specific class may already have a superclass? The solution is to add a parent class to its parent class, or add a parent class to its parent class, and only move to the top of the class hierarchy. In this way, the design of insertability of a specific class becomes a modification of all classes in the entire hierarchy.
The interface is a guarantee of insertability.
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, and of course they can also choose to replace all these methods, or some of them. At this time, these subclasses are pluggable (and can be loaded with this interface type, passing and implementing all its subclasses).
What we care about is not the specific class, but whether this class implements the interface we need.
The interface provides interpolation and method calls. The larger the scale of the software system, the longer the life cycle. The interface ensures the flexibility and scalability of the software system and the interpolationability.
type
Use Java interface to couple software units internally and externally. Using Java interfaces is not a specific class to perform type declaration of variables, return type declaration of method, type declaration of parameter, and conversion of data types.
In ideal cases, a specific Java class should only implement the methods declared in the Java interface and abstract Java class, and should not give extra methods.
Type hierarchy
Java interfaces (and abstract classes) are generally used as the starting point for a type of hierarchy.
If a class already has a major supertype, then by implementing an interface, the class can have another minor supertype, which is called a hybrid type.
Common methods of Java interface
Single method interface
public interface Actionlistener(){
public abstract void actionPerformed(ActionEvent event);
}
Only one method is used. Only when this interface is implemented (rewrite the only method in this interface) can you be qualified to register in the event listener list (the parameter is Actionlistener type). When the event source changes, this unique actionPerformed method will be automatically called.
Identification interface
It is an interface without any methods and attributes. The identification interface does not have any semantic requirements for the class that implements it, it simply indicates that the class that implements it belongs to a specific type (passing).
It is not recommended to use too many identification interfaces.
Constant interface
Use the Java interface to declare some constants, and then use these constants by the class that implements this interface (this did this before when doing artboards). It is recommended not to imitate this constant interface.
Java language type safety issues
Java is a strongly typed language. This means that the Java compiler will check the code to determine that there is no assignment and that each method call is of type. If there are any cases that do not match, 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 constructor declaration gives the method features. 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.
Java language is type-safe. That is to say, any legitimate Java class accepted by a Java compiler is guaranteed to be type-safe. In other words, there will be no errors of any kind during the program run. It is impossible for a Java program to treat a variable that originally belongs to one type as another type, so there will be no errors caused by this.
Simply put, Java language relies on three mechanisms to achieve type safety: type checking during compilation, automatic storage management, and array boundary checking.
Note: Most of this article is written by Teacher Yan Hong's "Java and Mode".
What is an interface?
Interfaces in Java are declarations of a series of methods, and are a collection of method features. An interface only has the characteristics of methods but no method implementation. Therefore, these methods can be implemented by different classes in different places, and these implementations can have different behaviors (functions).
There are two meanings of interfaces: one, Java interface, the structures that exist in the Java language have specific syntax and structure; two, the feature set of methods that a class has 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, and do not include the return type of the method, the name of the parameter, and the thrown exception. When the Java compiler checks the overload of a method, it will determine whether the two methods are overloaded methods based on these conditions. However, when the Java compiler checks for the permutation of the method, it will further check whether the return type and thrown exception of the two methods (subtypes and subtypes) are the same.
The rules for interface inheritance and implementation of 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 appearances, but only describes public behavior, so the Java interface is more abstract than the Java abstract class.
The methods of Java interface can only be abstract and public. The Java interface cannot have a constructor. The Java interface can have public, static and final properties.
The interface separates the characteristics of the method and the implementation of the method. This segmentation is reflected in that the interface often represents a role, which wraps the operations and attributes related to the role, and the class that implements the interface is the actor who plays the role. A role is played by different actors, and different actors do not require any other commonalities except playing a common role.
Why use interface?
Two similar functions in the two classes call their class dynamically to determine an implementation, and they provide an abstract parent class, and the child class implements the methods defined by the parent class respectively.
Problem: Java is a single inheritance language. Generally speaking, which specific class may already have a superclass? The solution is to add a parent class to its parent class, or add a parent class to its parent class, and only move to the top of the class hierarchy. In this way, the design of insertability of a specific class becomes a modification of all classes in the entire hierarchy.
The interface is a guarantee of insertability.
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, and of course they can also choose to replace all these methods, or some of them. At this time, these subclasses are pluggable (and can be loaded with this interface type, passing and implementing all its subclasses).
What we care about is not the specific class, but whether this class implements the interface we need.
The interface provides interpolation and method calls. The larger the scale of the software system, the longer the life cycle. The interface ensures the flexibility and scalability of the software system and the interpolationability.
type
Use Java interface to couple software units internally and externally. Using Java interfaces is not a specific class to perform type declaration of variables, return type declaration of method, type declaration of parameter, and conversion of data types.
In ideal cases, a specific Java class should only implement the methods declared in the Java interface and abstract Java class, and should not give extra methods.
Type hierarchy
Java interfaces (and abstract classes) are generally used as the starting point for a type of hierarchy.
If a class already has a major supertype, then by implementing an interface, the class can have another minor supertype, which is called a hybrid type.
Common methods of Java interface
Single method interface
public interface Actionlistener(){
public abstract void actionPerformed(ActionEvent event);
}
Only one method is used. Only when this interface is implemented (rewrite the only method in this interface) can you be qualified to register in the event listener list (the parameter is Actionlistener type). When the event source changes, this unique actionPerformed method will be automatically called.
Identification interface
It is an interface without any methods and attributes. The identification interface does not have any semantic requirements for the class that implements it, it simply indicates that the class that implements it belongs to a specific type (passing).
It is not recommended to use too many identification interfaces.
Constant interface
Use the Java interface to declare some constants, and then use these constants by the class that implements this interface (this did this before when doing artboards). It is recommended not to imitate this constant interface.
Java language type safety issues
Java is a strongly typed language. This means that the Java compiler will check the code to determine that there is no assignment and that each method call is of type. If there are any cases that do not match, 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 constructor declaration gives the method features. 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.
Java language is type-safe. That is to say, any legitimate Java class accepted by a Java compiler is guaranteed to be type-safe. In other words, there will be no errors of any kind during the program run. It is impossible for a Java program to treat a variable that originally belongs to one type as another type, so there will be no errors caused by this.
Simply put, Java language relies on three mechanisms to achieve type safety: type checking during compilation, automatic storage management, and array boundary checking.
Note: Most of this article is written by Teacher Yan Hong's "Java and Mode".