Saturday, September 4, 2010

java interface, and its benefits

 interface is achieved can be inserted in the key component, can be inserted into the key component there is a common interface, and each component implements this interface. What is Interface?Java, the interface is a series of method statements, are some ways to feature a collection of the characteristics of an interface method is not only a method to achieve, these methods can be in different places by different classes to achieve, and these implementations can have different behavior (function). ... Interface elements can be inserted in the realization of the key, the key component can be inserted there is a common interface, and each component implements this interface.

What is Interface?
Java, the interface is a series of method statements, are some ways to feature a collection of the characteristics of an interface method is not only a method to achieve, these methods can be different in different parts of the class to achieve, and these implementations can have different behavior (function).
Interface of two meanings: one, Java interfaces, Java language, there is structure, there is a specific syntax and structure; 2, a class method with the characteristics of the collection is a logical abstraction. The former is called "Java interface", which is called "interface."

In the Java language specification, a method of characteristics including the method name only, the number and types of parameters, but not the method return type, parameter name and the thrown exception.Examination methods in the Java compiler when overloaded, under these conditions will determine whether the two methods is overloaded methods. However, examination methods in the Java compiler when replacement will further examine the two methods (super-types and sub-sub-office type) return type and exception thrown is the same.

Interface inheritance and implementation inheritance rules different from a class that only a direct parent class, but can implement multiple interfaces. No Java interface itself to achieve, because Java interfaces do not relate to appearance, but only describe the public behavior, so Java Java interface, abstract class is more than the abstract. Java interface method can only be abstract and open, Java interfaces can not have constructors, Java interfaces can be public, static and final attributes.

Interface, the method of characteristics and methods of achieving separated. This division is often reflected in the interface represents a role, it is packed with the role of the related operations and attributes, and to achieve this interface is the actor playing this role. A role to play by different actors, and between different actors play a common role in addition to outside, does not require the other common.

Why use an interface?
Two classes of two similar functions, call them the decision of an implementation class dynamic, that they provide an abstract parent class, subclass were defined to achieve the parent class method.
The emergence of the problem: Java is a single inheritance language, under normal circumstances, what specific class may already have a super class, solution is to add its parent class parent, or to its parent parent parent class plus , only to move to the top of class hierarchy. Thus, for a specific class can be inserted in the design , it becomes all the whole class hierarchy changes.

Interface is of guarantees can be inserted.
In a hierarchical structure of any one class can implement an interface that will affect all of these sub-classes, but will not affect any of these super-class. These will have to implement this interface, the method provided, and subclass Ke Yi automatically inherited from such methods, of course, can also select the replacement out All these methods, Huozhe them to certain methods, this time, these sub-classes have that can be inserted in (and can use this interface type loading, pass all the child to achieve his class).

Our concern is not that a specific class, but whether this class implements the interface we need.
Interface provides the method call on the association, and can be inserted in the software system, the larger the longer the life cycle of the interface makes the software system flexibility and scalability can be ensured in terms of insertion.

Type
Using the Java interface, the software units together with internal and external coupling. Interfaces using the Java class is not a specific type of variable declarations, method return type declarations, type declarations parameters, and data type conversion.
In the ideal case, a specific Java class should be achieved only Java interfaces and abstract methods declared in Java classes, and should not be a redundant way.

Type hierarchy
Java interfaces (and abstract classes) is generally used as a type of hierarchical structure of the starting point.
If a class already has a major super-type, then by implementing an interface, this class can have other secondary supertype, which is called the secondary super-type mixed type.

Common method for Java Interface
Single method interfaces
public inte *** ce Actionlistener () (
public abstract void actionPerformed (ActionEvent event);
)
And only one way only, and only implement this interface (rewrite the interface in only one way), you are eligible to register the event listener list (parameters Actionlistener type), when the event source changes, automatic call The only actionPerformed method.

Identity Interface
Is no interface methods and properties. Interface does not realize that it identifies a class of any semantic requirements, it merely indicates that it belonged to achieve a particular type (transmission).
Does not recommend the use of excessive identification interface to.

Constant Interface
Using Java interfaces to declare some constants, then the class that implements the interface using these constant (when drawing board before doing've done it). Constant interface, not recommended imitation of this practice.

No comments:

Post a Comment