Detailed explanation of interfaces in Java

summary

Interface is a reference type in Java language and a collection of methods. If a class encapsulates member variables, construction methods and member methods, the interior of the interface mainly encapsulates methods, including abstract methods (JDK 7 and before), default methods and static methods (JDK 8), and private methods
(JDK 9).
The definition of interface, which is similar to the way of defining classes, but uses the interface keyword. It will also be compiled into class file, but make sure that it does not
Not a class, but another reference data type.

Reference data type: array, class, interface.

Interface, which cannot create objects, but can be implemented (similar to being inherited). A class that implements an interface (which can be regarded as a subclass of the interface) needs to implement all the abstract methods in the interface. After creating this class object, you can call the method, otherwise it must be an abstract class.

Define format

public interface interface name{
//Abstract method
//Default method
//Static method
//Private method
}

Contains abstract methods

Abstract method: use the abstract keyword to modify. It can be omitted. There is no method body. This method is used by subclass implementations.
The code is as follows:

public interface InterFaceName {
    public abstract void method();
}

Contains default and static methods

Default method: use the default modifier and cannot be omitted. It can be called or overridden by subclasses.
Static method: it is decorated with static and can be called directly by the interface.
The code is as follows:

public interface InterFaceName {
    public default void method() {
        // Execute statement
    }
    public static void method2() {
        // Execute statement
    }
}

Contains private methods and private static methods

Private method: private modification is used for calling the default method or static method in the interface.
The code is as follows:

public interface InterFaceName {
    private void method() {
        // Execute statement
    }
}

Basic implementation

Overview of implementation

The relationship between class and interface is implementation relationship, that is, class implements interface. This class can be called implementation class of interface or subclass of interface. The actions implemented are similar to inheritance and the format is similar, but the keywords are different. The implementation uses the implements keyword.
Non Abstract subclass implementation interface:

  1. You must override all abstract methods in the interface.
  2. Inherits the default method of the interface, which can be called directly or overridden.
    Implementation format:

Class class name implements interface name{
//Rewrite the abstract method in the interface [required]
//Override the default method in the interface [optional]
}

Use of abstract methods

It must be fully implemented. The code is as follows:
Define interface:

public interface LiveAble {
    // Define abstract methods
    public abstract void eat();
    public abstract void sleep();
}

Define implementation classes:

public class Animal implements LiveAble {
    @Override
    public void eat() {
        System.out.println("Eat something");
    }
    @Override
    public void sleep() {
        System.out.println("sleep");
    }
}

Define test class:

public class InterfaceDemo {
    public static void main(String[] args) {
        // Create subclass object
        Animal a = new Animal();
        // Call the implemented method
        a.eat();
        a.sleep();
    }
}

Default use method

You can inherit, override, or choose one, but you can only call it through the object that implements the class.

  1. Inherit the default method. The code is as follows:

Define interface:

public interface LiveAble {
    public default void fly(){
        System.out.println("Fly in the sky");
    }
}

Define implementation classes:

public class Animal implements LiveAble {
    // Inheritance, nothing to write, direct call
}

Define test class:

public class InterfaceDemo {
    public static void main(String[] args) {
        // Create subclass object
        Animal a = new Animal();
        // Call default method
        a.fly();
    }
}
Output result:
Fly in the sky
  1. Override the default method with the following code:

Define interface:

public interface LiveAble {
    public default void fly(){
        System.out.println("Fly in the sky");
    }
}

Define implementation classes:

public class Animal implements LiveAble {
    @Override
    public void fly() {
        System.out.println("Fly freely");
    }
}

Define test class:

public class InterfaceDemo {
    public static void main(String[] args) {
        // Create subclass object
        Animal a = new Animal();
        // Call default method
        a.fly();
    }
}
Output result:
Fly freely

Use of static methods

Static vs The class file is related, and can only be called by the interface name, not by the class name of the implementation class or the object of the implementation class. The code is as follows:
Define interface:

public interface LiveAble {
    public static void run(){
        System.out.println("Run~~~");
    }
}

Define implementation classes:

public class Animal implements LiveAble {
    // Static methods cannot be overridden
}

Define test class:

public class InterfaceDemo {
    public static void main(String[] args) {
        // Animal.run(); //  [error] method cannot be inherited or called
        LiveAble.run(); //
    }
}
Output result:
Run~~~

Use of private methods

Private method: only the default method can be called.
Private static methods: default methods and static methods can be called.
If there are multiple default methods in an interface and there are duplicate contents in the methods, they can be extracted and encapsulated into private methods for the default methods to call. From a design perspective, private methods are an aid to default and static methods. Students can test by themselves on the basis of the technology they have learned.
Define interface:

public interface LiveAble {
    default void func(){
        func1();
        func2();
    }
    private void func1(){
        System.out.println("Run~~~");
    }
    private void func2(){
        System.out.println("Run~~~");
    }
}

Multiple implementation of interface

In the inheritance system, a class can inherit only one parent class. For interfaces, a class can implement multiple interfaces, which is called multiple implementation of interfaces. Moreover, a class can inherit a parent class and implement multiple interfaces at the same time.
Implementation format:

class Class name [extends Parent class name] implements Interface name 1,Interface name 2,Interface name 3... {
    // Rewrite the abstract method in the interface [required]
    // Override the default method in the interface [optional if there is no duplicate name]
}

[]: indicates optional operation.

Abstract method

When there are multiple abstract methods in the interface, the implementation class must override all abstract methods. If the abstract method has duplicate names, it only needs to be rewritten once. The code is as follows:
Define multiple interfaces:

interface A {
    public abstract void showA();
    public abstract void show();
}
interface B {
    public abstract void showB();
    public abstract void show();
}

Define implementation classes:

public class C implements A,B{
    @Override
    public void showA() {
        System.out.println("showA");
    }
    @Override
    public void showB() {
        System.out.println("showB");
    }
    @Override
    public void show() {
        System.out.println("show");
    }
}

Default method

When there are multiple default methods in the interface, the implementation class can inherit and use them. If the default method has duplicate names, it must be overridden once. The code is as follows:
Define multiple interfaces:

interface A {
    public default void methodA(){}
    public default void method(){}
}
interface B {
    public default void methodB(){}
    public default void method(){}
}

Define implementation classes:

public class C implements A,B{
    @Override
    public void method() {
        System.out.println("method");
    }
}

Static method

Static methods with the same name in the interface will not conflict because they can only be accessed through their respective interface names.

Priority issues

When a class inherits a parent class and implements several interfaces, the member method in the parent class has the same name as the default method in the interface, and the subclass selects the member method of the parent class nearby. The code is as follows:
Define interface:

interface A {
    public default void methodA(){
        System.out.println("AAAAAAAAAAAA");
    }
}

Define parent class:

class D {
    public void methodA(){
        System.out.println("DDDDDDDDDDDD");
    }
}

Define subclasses:

class C extends D implements A {
    // methodA method is not overridden
}

Define test class:

public class Test {
    public static void main(String[] args) {
        C c = new C();
        c.methodA();
    }
}

Output results:
DDDDDDDDDDDD

Multiple inheritance of interfaces

One interface can inherit another or more interfaces, which is similar to the inheritance between classes. Interface inheritance uses the extends keyword, and sub interfaces inherit
Method of the parent interface. If the default method in the parent interface has a duplicate name, the child interface needs to be rewritten once. The code is as follows:
Define parent interface:

interface A {
    public default void method(){
        System.out.println("AAAAAAAAAAAAAAAAAAA");
    }
}
interface B {
    public default void method(){
        System.out.println("BBBBBBBBBBBBBBBBBBB");
    }
}

Define sub interfaces:

interface D extends A,B{
    @Override
    public default void method() {
        System.out.println("DDDDDDDDDDDDDD");
    }
}

Tips:
When the sub interface overrides the default method, the default keyword can be retained.
When subclasses override the default method, the default keyword cannot be reserved.

Characteristics of other members

In the interface, member variables cannot be defined, but constants can be defined, and their values cannot be changed. public static final is used by default.
There is no constructor in the interface, so the object cannot be created.
There is no static code block in the interface.

Tags: Java

Posted by kiwi_uk on Fri, 15 Apr 2022 16:04:57 +0930