Java language interface and type-safe _jsp programming

Source: Internet
Author: User
Tags abstract event listener inheritance
Interface is the key to achieve the pluggable nature of components, the key to pluggable components 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, a collection of method features, an interface that has no method to implement the characteristics of a method, so that these methods can be implemented in different places by different classes, and these implementations can have different behaviors (functions).

The two meanings of the interface are: One, Java interface, the structure in the Java language, the specific syntax and structure, and the characteristic set of the method that a class has, which is a logical abstraction. The former is called "Java Interface", the latter is called "interface".

In the Java language Specification, a method's characteristics include only the name of the method, the number and type of parameters, and not the return type of the method, the name of the parameter, and the exception thrown out. When the Java compiler checks the overload of a method, it determines whether the two methods are overloaded methods based on these conditions. However, when the Java compiler checks the substitution of methods, it further checks that the return type of the two methods (the branch supertype and subtype) is the same as the thrown exception.

interface inheritance and implementation inheritance rules differ, a class has only one direct parent class, but can implement multiple interfaces.

The Java interface itself has no implementation, because the Java interface does not involve representations and only describes public behavior, so Java interfaces are more abstract than Java abstract classes.

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

The interface separates the feature of the method from the implementation of the method. This segmentation is reflected in the interface that often represents a role that wraps the actions and attributes associated with the role, and the class that implements the interface is the actor who plays the role. A character is played by different actors, and different actors do not require anything in common except to play a common role.

  Why use an interface?

Two of the two similar functions in the class, call their class dynamic decision an implementation, that they provide an abstract parent class, the subclass implements the method defined by the parent class, respectively.

The problem arises: Java is a single inherited language, in general, which specific class may already have a superclass, the solution is to its parent class plus parent class, or to its parent class and parent class, only to move to the top of class hierarchy structure. Thus, the design of the pluggable nature of a specific class becomes a modification of all classes in the entire hierarchy.

Interfaces are guaranteed to be pluggable.

Any class in a hierarchy can implement an interface that affects all subclasses of the class, but does not affect any superclass of the 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, you can also choose to replace all of these methods, or some of them, at which point the subclass has the pluggable (and can be loaded with this interface type, passing all of his subclasses).

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

Interface provides the association and the pluggable nature of the method invocation, the larger the software system, the longer the lifecycle, the interface makes the software system flexible and scalable, and the pluggable aspect is ensured.

Type

Use the Java interface to couple software units with internal and external. Using a Java interface is not a specific class for variable type declarations, method return type declarations, parameter type declarations, and data type conversions.

Ideally, a specific Java class should implement only the methods declared in the Java interface and abstract Java classes, not the redundant methods.

   type hierarchy Structure

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

If a class already has a major super type, then by implementing an interface, the class can have another minor superclass, which is called a mixed type.

  Java Interface Common methods

Single Method interface

public interface ActionListener () {

public abstract void actionperformed (ActionEvent event);

}

Only and only one method, only if you implement this interface (overriding the only method in this interface), are you eligible to register in the event listener list (the parameter is ActionListener type) and automatically invoke the unique Actionperformed method when the event source changes.

Identifying interfaces

is an interface without any methods and properties. The identity 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 (transitive).

Excessive use of identity interfaces is not recommended.

Constant interface

Use the Java interface to declare constants, and then use these constants for the classes that implement the interface (previously done on the artboard). It is recommended that you do not imitate this constant interface approach.

   Java language type security issues

Java is a strongly typed language. This means that the Java compiler checks the code to make sure that it is not assigned once, and that the invocation of each method conforms to the type. The Java compiler gives an error if there is any situation that does not match.

Type checking is based on the simple fact that each variable's declaration gives the variable a type; Each method, including the constructor's declaration, gives the method a characteristic. In this way, the Java compiler can infer an obvious type for any expression, and the Java compiler can examine the type based on the obvious type.

The Java language is type-safe. This means that any legitimate Java class that is accepted by the Java compiler is type-safe. In other words, there will not be any type of error while the program is running. It is not possible for a Java program to treat a variable that was originally a type as another type, so there is no error resulting from it.

Simply put, the Java language relies on three mechanisms for type safety: type checking during compilation, automatic storage management, and boundary checking of arrays.

Note: This large part of the content of the Shanhong teacher's "Java and 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.