Java basic knowledge notes 3-interface

interface

Overview

An interface is a common specification of multiple classes.

Jikou is a reference data type, and the most important content is the abstract method.

How to define the format of an interface:

public interface interface name {

​ //Interface content

}

public interface InterfaceDemo01 {
    
    
    //以下四种定义抽象方法都可以
    public abstract void methodAbs1();

    abstract void methodAbs2();

    public void methodAbs3();

    void methodAbs4();
}

Precautions:

1. For the abstract method in the interface, the modifier must be two fixed keywords: public abstract

2. These two keyword modifiers can be optionally omitted. (Not recommended for beginners)

3. The three elements of the method can be defined at will.

Note: After replacing the keyword interface, the bytecode Wenjie generated by the compilation is still: .java——>.class

If it is Java 7, then the contents that can be included in the interface are:

1. Constant

2. Abstract method

If it is Java 8, it can additionally include:

3. The default method

4. Static method

If it is Java 9, you can additionally include:

5. Private method

Interface use steps

1. The interface cannot be used directly, there must be an "implementation class" to "implement" the interface.

format:

public class implementation class name implements interface name {

​ //Content

}

2. The implementation class of the interface must override all abstract methods in the interface.

Implementation: Remove the abstract keyword and add the method body braces.

3. Create an object of the implementation class and use it.

The default method of the interface

1. The default method of the interface can be directly called by implementing the class object.

2. The default method of the interface can also be overridden by the interface implementation class. (Implementation class overrides the default method of interface)

Static method

Starting from Java 8. Static methods are allowed in the interface.

format:

public static return value type method name (parameter list) {

​ //Content

}

Just replace abstract or default with static and bring the method body.

Note: You cannot call static methods in the interface through the object of the interface implementation class.

Correct usage: call the static method directly through the interface name.

Format: Interface name. Static method name (parameter);

public interface InterfaceDemo03 {
    
    
    public static void methodStatic(){
    
    
        System.out.println("静态方法执行成功");
    }
}

public class InterfaceDemo03Impl implements InterfaceDemo03{
    
    

}


public class InterfaceDemo03Main {
    
    
    public static void main(String[] args) {
    
    

//        InterfaceDemo03Impl inter = new InterfaceDemo03Impl();
//        inter.methodStatic();    该写法错误

        InterfaceDemo03.methodStatic();
    }
}

Private method

Problem Description:

We need to extract a public method to solve the problem of duplicate code between the two default methods.

But this shared method should not be used by the implementation class, it should be privatized. (Private methods can only be called by the interface itself, and cannot be used by implementation classes or others)

Solution:

Starting from Java 9, private methods are allowed in interfaces.

1. Ordinary private methods, to solve the problem of repeated code between multiple default methods

format:

private return value type method name (parameter list) {

​ Method body

}

2. Static private method: solve the problem of repeated code between multiple static methods

format:

private static return value type method name (parameter list) {

​ Method body

}

Interface constant

"Member variables" can also be defined in the interface, but they must be modified with the three keywords of public static final.

From the effect point of view, this is actually the [constant] of the interface.

Format: public static final data type constant name = data value;

Note: Once the final keyword is used for modification, the description cannot be changed.

Precautions:

1. For the constants in the interface, any one or more of the public static final can be omitted, even if it is not written. Write it all for beginners.

2. The constants in the interface must be assigned, and must not be assigned.

3. The name of the constant in the interface, use completely uppercase letters, separated by underscore. (It is recommended that the naming rules are all uppercase, separated by underscores)

Interface and class

1. The interface has no static code block or construction method.

2. The direct parent of a class is unique, but a class can implement multiple interfaces at the same time.

format:

public class MyInterfaceImpl implements MyInterfaceA,MyInterfaceB{
    
    
    @Override
    public void method() {
    
    
        System.out.println("对多个接口当中冲突默认方法进行了覆盖重写实现");
    }
}

3. If there are repeated abstract methods among the multiple interfaces implemented by the implementation class, you only need to overwrite it once.

4. If the implementation class does not cover all abstract methods in all interfaces, then the implementation class must be an abstract class.

5. If there are duplicate default methods among the multiple interfaces implemented by the implementation class, the implementation class must override the conflicting default methods.

6. If a method in the direct parent class of a class conflicts with the default method in the interface, the method in the parent class is preferred.

Multiple inheritance of interfaces

1. There is single inheritance between classes, and there is only one direct parent class.

2. There are multiple implementations between classes and interfaces. A class can implement multiple interfaces.

3. There is multiple inheritance between interfaces.

Precautions:

1. It does not matter if the abstract methods in multiple parent interfaces are repeated.

2. If the default method in multiple interfaces is repeated, then the sub-interface must be overwritten by the default method, [and must carry the default keyword]

Guess you like

Origin blog.csdn.net/weixin_43215322/article/details/108675214