The Java interface

Source: Internet
Author: User
Tags naming convention

Java Interface (Interface), is a series of methods of Declaration, is a collection of methods features , an interface only the method of the characteristics of the method is not implemented, the interface is a Java provided by an object-oriented mechanism ( It is like a collection of definitions of abstract methods and constant values, stating that methods and classes are declared in the same way. 

< modifier >interface< Interface name >{
[< constant declaration;]
[< abstract method declaration;]

An interface is a precondition for type conversion and a guarantee of dynamic invocation. A type conversion (multiple inheritance) is accomplished by implementing an interface, and the dynamic invocation only cares about the type and does not care about the specific class.

all the methods in the interface are abstract, and the methods in the interface are public,abstract by default, so you can omit 2 modifiers, but because of this, you cannot use and public,abstract conflicting methods, and you can declare constants in the interface. , but cannot declare instance variables.
inheritance in Java can only support single inheritance, so. The provision of interfaces provides a path for us to implement multiple inheritance, so it is convenient to understand it, so that these methods can be implemented in different classes in various places, and these implementations can have different behaviors (functions).

I. Interface meaning:

1.Java interface, the structure that exists in the Java language, has a specific syntax and structure; 2. The characteristic set of methods that a class has, is a logical abstraction.

The former is called "Java Interface", which is called "interface".

The Java interface itself has no implementation, because the Java interface does not involve appearances, but only describes the public behavior, so the Java interface is more abstract than the Java abstract class.
Java interface methods can only be abstract and public, Java interfaces cannot have constructors, Java interfaces can have public, static and final properties.
two. Why interface Java is a single-inheritance language, to add new functionality to a specific class that already has a parent class, under the OCP principle, the resolution is to add the parent class to its parent class, or to the parent class of its parent class, until it moves to the top of the class hierarchy. In this way, the design of the pluggable nature of a specific class becomes a modification to all classes in the hierarchy.
when you have an interface, in the above example, you do not need to maintain all classes in the entire hierarchy.
three. Interface with HU may insert:
any class within a hierarchy can implement an interface that affects all subclasses of this class, but does not affect any superclass 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, of course, you can also choose to displace all of these methods, or some of these methods, at this time, these subclasses have the pluggable (and can be loaded with this interface type, passing implements all of his subclasses).
interface provides the correlation and the pluggable on the method call, the larger the scale of the software system, the longer the life cycle, the interface makes the software system flexible and extensible, the pluggable aspect is guaranteed.
It is with the interface that the Java single inheritance has the possibility of a new extension (to achieve multiple inheritance); iii. type hierarchy Java interfaces (and abstract classes) are generally used as a starting point for a hierarchy of a type.
if a class already has a major super-type, by implementing an interface, the class can have another secondary superclass, which is called a mixed type.
four. Java Interface classification 1, common interface (contains method definition) public interface actionlistener{public abstract void actionperformed (ActionEvent event);}
2. The identity interface (without any method and attribute definitions) identifies an interface that has no 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 particular type.
Public interface Serializable{};3, constant interfaces refer to the use of Java interfaces to declare constants, which are then used by classes that implement this interface.
Public interface appconstants{public static final data_source_name= "test";p ublic static final user_name= "test"; public static final password= "Test";}
Five. features of the interface 1, the Java interface member variables by default are public,static,final type (can be omitted), must be displayed initialization, that is, the interface member variable is a constant (uppercase, the words are separated by "_")

2, the Java interface method is the default public,abstract type (can be omitted), there is no method body, cannot be instantiated
3. The Java interface can only contain member variables of type public,static,final and member methods of type Public,abstract
4, there is no construction method in the interface, cannot be instantiated
5, one interface cannot implement (implements) another interface, but it can inherit many other interfaces
6. The Java interface must implement its abstract method through a class
7 . When a class implements a Java interface, it must implement all the abstract methods in the interface, otherwise the class must be declared as an abstract class
8, the creation of an instance of an interface (instantiation) is not allowed, but it allows to define a reference variable of the interface type that references an instance of the class that implements the interface
9, A class can only inherit a direct parent class, but can implement multiple interfaces, the indirect implementation of multiple inheritance.

10, is the implementation of the class must implement the method defined in the interface;

six. Java interface and Java abstract class The emphasis of object-oriented design is abstraction . Abstract classes and interfaces are located on top of the inheritance tree.

Same point:

1, representing the abstraction layer of the system, when a system uses a class on an inheritance tree, it should try to declare the reference variable as the upper-level abstract type of the inheritance tree, which can improve the coupling between the two systems.

2, can not be instantiated

3, all contain abstract methods, these abstract methods are used to describe what the system can provide services, but does not include the method body different points:
1, The biggest difference is that Java abstract classes  can provide some of the methods of implementation , and the Java interface can not This is probably the only advantage of the Java abstract class, but this advantage is very useful. 
can add a new concrete method to the abstract class, all subclasses automatically get this method, but add a new method in the Java interface, all the classes that implement this interface cannot be compiled successfully, must manually give each class that implements the interface the implementation of the method; 2. An implementation of an abstract class can only be given by subclasses, which means that the implementation is only in the hierarchy of inheritance that is defined by the abstract class, so the performance of the abstract class as a type definition tool is greatly compromised.
Java interface, any class that implements a method specified by a Java interface can have the type of this interface, and a class can implement any number of Java interfaces, so there are many types of this class.
See above: The Java interface is an ideal tool for defining mixed types, and a hybrid class indicates that a class does not only have the behavior of one of the main types, but also has other minor behaviors.
3, the combination of 1, 2 points in the abstract class and Java interface of their respective advantages, with refined design mode is out:
the work of declaring type is still assumed by the Java interface, but given a Java abstract class, and the implementation of this interface, and other specific classes belonging to this abstract type can choose to implement this Java interface, you can also choose to inherit this abstract class, that is, in the hierarchy, Java interface at the top, followed by the abstract class, the next two of the biggest advantages can be played to the extreme. This mode is "default adaptation mode".
This pattern is used in the Java language API, and all follow a certain naming convention: Abstract + interface name.
Seven. General principles for using interfaces and abstract classes:
1, using the interface as a system to interact with the outside of the window stand outside the user (another system), the interface to the user to promise what the system can provide services, standing in the system itself, interface development system must achieve what services, An interface is the highest-level abstract type in a system. Interface interaction can improve the coupling between the two systems of system A through system B interaction, refers to system A to access system B, the reference variable is declared as the interface type in System B, the reference variable refers to the interface in system B an instance of the implementation class.
Public interface B {}
Public class C implements B {}
Public Class A {B a = new C ();}
2, the Java interface itself must be very stable, once the Java interface is formulated, it is not allowed to encounter more, otherwise the external users and the system itself to affect 3, with the abstract class to customize the expansion point of the system, abstract class to complete the partial implementation, but also some functions through its subclasses to achieve

The Java interface

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.