【Java】interface

Interface evolved from abstract classes.

An abstract class can contain one or more abstract methods. Even when there is no abstract method, this class can be declared as an abstract class to prevent its instantiation.

But in the interface, all methods must be abstract and cannot contain method bodies. It can be said that interfaces are more abstract than abstract classes. Use the interface keyword to declare.

Like classes, interfaces can be inherited, and interfaces can be inherited multiple times.


1. The interface cannot be instantiated. It is not allowed to use the new keyword to construct an instance of an interface. There is no construction method for interfaces.


2. Trying to define instance variables in the interface, non-abstract instance methods and static methods, are illegal.

1) The methods in the interface are public abstract by default.

Just imagine, if the methods in the interface can be declared as static,

Then it can be called by the class name .method(), but the method in the interface has no entity, so it is contradictory. How to call it?


2) The member variables declared in the interface are public static final by default, which are regarded as static constants and must be explicitly initialized.

So why must the member variables in the interface be public static final?

As a highly abstract template, an interface must be implemented to make sense, and it must be public at this level.

The attributes in the interface are also members of the template. In other words, all implementation classes of the interface should share this attribute, so it must be public static. 

Second, if non-final variables can be defined in the interface, and the methods are abstract, then there is a contradiction: there are member variables, but the method cannot operate these

Member variables. Although a certain implementation class can directly modify the value of these static member variables through the class name. variable, in this way, all the corresponding variable values ​​of the implementation class are

Modified, what is the difference between this and abstract class? Therefore, variables cannot appear in the interface. If there are variables, it is contradictory to the unified abstract idea proposed by the interface.

Therefore, the attributes in the interface must be constants, readable and not writable, so as to provide uniform attributes for the objects that implement the interface.

An interface is a higher level of abstraction, a specification and a declaration of function definitions. All variable things should be attributed to the implementation class, so that the interface can play a standard and standardized role.


3. An interface does not implement another interface, but can inherit multiple other interfaces. The multiple inheritance characteristics of the interface make up for the single inheritance of the class.

public interface SataHdd extends A, B{

    public static final int CONNECT_LINE=4;
    public void writeData(String data);
    public String readData();
}

interface A {
	public void a();
}
interface B {
	public void B();
}

4. A class can only inherit one parent class, but can implement multiple interfaces

The format of the implementation interface is as follows:
modifier class class name extends parent class implements multiple interfaces {     implementation method } If a class cannot implement all the abstract methods of the interface, then this class must be defined as an abstract method.



5. Why use interfaces? In the


development of large projects, it may be necessary to insert a class from the middle of the inheritance chain so that its subclasses have certain functions without affecting their parent class. For example, A -> B -> C -> D -> E. A is an ancestor class. If you need to add some common functions to classes C, D, and E, the easiest way is to let class C inherit another class. But here comes the problem. Java is a single-inheritance language. C can no longer inherit another parent class. It only moves to the top of the inheritance chain and let A inherit another parent class. In this way, the modifications to the C, D, and E classes affect the entire inheritance chain, and there is no pluggable design.

The interface is a guarantee of pluggability. Any class in an inheritance chain can implement an interface, which will affect all subclasses of this class, but not any parent class of this class. This class will have to implement the methods specified by this interface, and subclasses can automatically inherit these methods from this class. At this time, these subclasses are pluggable.

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

The interface provides the pluggability of association and method calls. The larger the scale of the software system, the longer the life cycle. The interface ensures the flexibility and scalability of the software system and the pluggability.

Interface occupies a pivotal position in object-oriented Java programming. In fact, one of the most important tasks in the design stage is to design the interfaces of each part, and then through the combination of interfaces, the basic framework structure of the program is formed.


6. The interface is used as a type. The

interface is used as a reference type. Any instance of a class that implements the interface can be stored in a variable of the interface type. Through these variables, you can access the methods in the interface implemented in the class. Java runtime The system dynamically determines which method in the class should be used, in fact, it calls the method of the corresponding implementation class.

public class Demo{
    public void test1(A a) {
        a.doSth();
    }
    public static void main(String[] args) {
        Demo d = new Demo();
        A a = new B();
        d.test1(a);
    }
}
interface A {
    public int doSth();
}
class B implements A {
    public int doSth() {
        System.out.println("now in B");
        return 123;
    }
}



Guess you like

Origin blog.csdn.net/michellechouu/article/details/48751033