Java interface and type security

Source: Internet
Author: User

Interfaces are the key to implementing component insertion. The key to inserting a component is that there is a public interface and each component implements this interface.

 What is an interface?

Interfaces in Java are a series of methods declarations and a set of methods features. An interface has only the features of a method and does not implement the methods, therefore, these methods can be implemented by different classes in different places, and these implementations can have different behaviors (functions ).

Two meanings of an interface: 1. Java interface, the structure existing in the Java language, with specific syntax and structure; 2. A feature set of a class's methods, is a logical abstraction. The former is called the "Java interface", and the latter is called the "interface ".

In the Java language specification, features of a method only include the method name, number and type of parameters, but not the return type of the method, parameter name, and thrown exceptions. When the Java compiler checks the method overloading, it determines whether the two methods are overloaded methods based on these conditions. However, when the Java compiler checks replacement of methods, it further checks whether the return type and thrown exception of the two methods (subclasses and subclasses) are the same.

Different from the rule for implementing inheritance, a class has only one direct parent class, but multiple interfaces can be implemented.

Java interfaces do not have any implementations, Because Java interfaces do not involve representations and only describe public behaviors. Therefore, Java interfaces are more abstract than Java Abstract classes.

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 features of the method from the implementation of the method. This split is embodied in the fact that an interface often represents a role, which encapsulates operations and attributes related to the role, and the class implementing this interface is the actors playing this role. A role is played by different actors. Different actors do not need to share a role with each other.

 Why use interfaces?

Two similar functions in the two classes call their classes to dynamically determine an implementation. Then they provide an abstract parent class, and subclass implements the methods defined by the parent class respectively.

The problem occurs: Java is a single-inherited language. Generally, a 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 the parent class of its parent class, and only move to the top of the class hierarchy. In this way, the design of the insertion ability of a specific class is changed to the modification of all classes in the overall hierarchy.

The interface is the guarantee of insertion.

In a hierarchical structure, any class can implement an interface, which affects all subclasses of this class, but does not affect any superclasses of this class. This class will have to implement the methods specified by this interface, and its subclass can automatically inherit these methods from this class, of course, you can also replace all these methods, or some of them, at this time, these subclasses are pluggable (and can be loaded with this interface type, which is passed to implement all its subclasses ).

We are not concerned about the specific class, but whether the class implements the interface we need.

Interfaces provide the scalability of association and method calling. The larger the scale of the software system, the longer the lifecycle. The interface ensures the flexibility and scalability of the software system and the maintainability.

Type

Use the Java interface to couple the software unit with the internal and external. Using Java interfaces is not a specific class for variable type declaration, return type declaration of methods, parameter type declaration, and data type conversion.

Ideally, a specific Java class should only implement the Java interface and the Methods declared in the abstract Java class, rather than the redundant methods.

  Type level structure

Java interfaces (and abstract classes) are generally used as the starting point of a class level structure.

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

 Common Java interface methods

Single Method Interface

Public interface Actionlistener (){

Public abstract void actionreceivmed (ActionEvent event );

}

There is only one method. You are eligible to register in the event listener list (the parameter is Actionlistener type) only when this interface is implemented (the only method in this interface is rewritten ), when the event source changes, the unique actionreceivmed method is automatically called.

Id Interface

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

Too many ID interfaces are not recommended.

Constant Interface

Use the Java interface to declare some constants, and then use these constants by the class implementing this interface (previously this was done when drawing board ). We recommend that you do not 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 no value is assigned once. Each method call is of the correct type. If there are any nonconformities, the Java compiler will give an error.

The type check is based on the simple fact that every declaration of a variable gives this variable a type, and every method, including the declaration of the constructor, gives the features of this method. In this way, the Java compiler can infer an explicit type for any expression, and the Java compiler can check the type based on the explicit type.

Java is type-safe. This means that any valid Java class accepted by the Java compiler is type-safe. In other words, there will be no errors of any type during the program running. A Java program cannot treat a variable of the same type as another type at all, so it will not produce any errors caused by this.

Simply put, Java relies on three mechanisms to achieve type security: type check during compilation, automatic storage management, and array boundary check.

Note: For most of the content in this article, I wrote "Java and mode".

Related Article

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.