Java-based interfaces and multiple inheritance of interfaces

What is an interface?

A Java interface is a series of method declarations, a collection of some method characteristics. An interface has only method characteristics but no method implementations. Therefore, these methods can be implemented by different classes in different places, and these implementations can have different behaviors ( Features).

It can be understood that an interface is a common specification of multiple classes, an interface is a reference data type, and the most important content is one of them: abstract methods.

The interface in Java 9 can include:

1. 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;
Remarks:
Once the final keyword is used for modification, it cannot be changed.

Precautions:

  1. The constants in the interface can be omitted public static final. Note: The same is true if you don't write it.
  2. The constants in the interface must be assigned; they must be assigned.
  3. The names of the constants in the interface, use completely uppercase letters, separated by underscores. (Recommended naming rules)

2. Abstract methods

In any version of Java, an interface can define abstract methods.
Format:
public abstract return value type method name (parameter list);

Precautions:

  1. For abstract methods in the interface, the modifier must be two fixed keywords: public abstract
  2. These two keyword modifiers can be optionally omitted. (Just learned today, so it is not recommended.)
  3. The three elements of the method can be defined at will.

3. The default method

Starting from Java 8, default methods are allowed to be defined in interfaces.
Format:
public default return value type method name (parameter list) { method body }

Note: The default method in the interface can solve the problem of interface upgrade.

4. Static methods

Starting from Java 8, static methods are allowed to be defined in interfaces.
Format:
public static Return value type Method name (parameter list) { Method body } Tip: Just replace abstract or default with static, and bring the method body.


5. Private methods

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

  1. Ordinary private method, to solve the problem of code duplication between multiple default methods
    Format:
    private return value type method name (parameter list) { method body }

  2. Static private method, to solve the problem of code duplication between multiple static methods
    Format:
    private static return value type method name (parameter list) { method body }

Define the format of an interface:

public interface interface name { // interface content } Ps: After replacing the keyword interface, the compiled bytecode file is still: .java --> .class.


Interface use (implementation) steps:

  1. The interface cannot be used directly, there must be an "implementation class" to "implement" the interface.
    Format:
    public class implements class name implements interface name { //… } One class can also implement multiple interfaces public class implements class name implements interface name 1, interface name 1 { //… }





  2. The implementation class of the interface must override all abstract methods in the interface.
    Implementation: Remove the abstract keyword and add method body braces.
  3. Create an object of the implementation class and use it.

Note:
If the implementation class does not cover all abstract methods in the override interface, then the implementation class itself must be an abstract class.

When using the interface, you need to pay attention to:

  1. The interface has no static code block or construction method!
    public interface MyInterfaceA {static {} }x
    public interface MyInterfaceA {public MyInterfaceA() {}} x
  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 and rewrite all abstract methods}
  3. If there are repeated abstract methods among the multiple interfaces implemented by the implementation class, you only need to overwrite and rewrite once.
  4. If the implementation class does not override all abstract methods in all interfaces, then the implementation class must be an abstract class.
  5. If there are duplicate default methods among multiple interfaces implemented by the implementation class lock, the implementation class must override the conflicting default methods.
  6. If a method in the direct parent class conflicts with the default method in the interface, the method in the parent class will be used first.

Multiple inheritance between interfaces

  1. There is single inheritance between classes. 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 doesn't matter if the abstract methods in multiple parent interfaces are repeated.
  2. If the default method in multiple parent interfaces is repeated, then the sub-interface must be overridden by the default method, [and with the default keyword].
public interface MyInterfaceA {

    public abstract void methodA();

    public abstract void methodCommon();

    public default void methodDefault() {
        System.out.println("AAA");
    }

}


public interface MyInterfaceB {

    public abstract void methodB();

    public abstract void methodCommon();

    public default void methodDefault() {
        System.out.println("BBB");
    }

}


public interface MyInterface extends MyInterfaceA, MyInterfaceB {

    public abstract void method();

    @Override
    public default void methodDefault() {

    }
}


public class MyInterfaceImpl implements MyInterface {
    @Override
    public void method() {

    }

    @Override
    public void methodA() {

    }

    @Override
    public void methodB() {

    }

    @Override
    public void methodCommon() {

    }
}

How many methods are there in the MyInterface interface above? 4
methodA comes from interface A
methodB comes from interface B
methodCommon also comes from interface A and B
method comes from itself

Guess you like

Origin blog.csdn.net/weixin_51492999/article/details/115059121