interface and type safety of Java language

Source: Internet
Author: User
Tags abstract 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.

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.