What is the use of the default method in the interface!

Hello, I am Xiao Suoqi, what is the use of the interface default method?

Let's talk about this topic and give you an example

The default method in the interface refers to the specific method implemented in the interface. They can add new methods to the existing interface without destroying the interface implementation.

Default methods allow interfaces to be extended without affecting existing code.

Before Java 8, only abstract methods could be defined in an interface, which means that a class implementing an interface must implement all methods.

However, Java 8 introduces the concept of default methods, so that interfaces can define concrete methods without the need for implementing classes to implement these methods.

The role of default methods is to allow the interface to be extended without breaking existing code, while also improving the readability and maintainability of the code.

Brief summary

The role of the default method mainly has the following aspects:

  1. Interface extensibility: The default method allows the interface to be extended without breaking existing code, which makes the interface more extensible.

  2. Code reusability: The default method can be shared in multiple interfaces, which can reduce code duplication and improve code reusability.

  3. Code readability and maintainability: Default methods can make code more readable and maintainable, because they can provide some commonly used implementations, thereby avoiding writing the same code repeatedly.

  4. Backward compatibility: Default methods can be added in interface version updates without affecting existing implementation classes, thereby maintaining backward compatibility.

give a chestnut

public interface MyInterface {
    void myMethod(); // 抽象方法

    default void myDefaultMethod() { // 默认方法
        System.out.println("这是一个默认方法");
    }
}

In this example, the interface MyInterfacedefines an abstract method myMethod()and a default method myDefaultMethod(). Classes that implement this interface can choose to implement myMethod()the method, or choose to use the default myDefaultMethod()method, so that you can avoid writing the same code repeatedly in each implementation class. For example:

public class MyClass implements MyInterface {
    public void myMethod() {
        System.out.println("这是一个实现了抽象方法的类");
    }
}

public class MyOtherClass implements MyInterface {
    // MyOtherClass 没有实现 myMethod() 方法,所以会使用默认的 myDefaultMethod() 方法
}

public class Main {
    public static void main(String[] args) {
        MyClass obj1 = new MyClass();
        obj1.myMethod(); // 输出 "这是一个实现了抽象方法的类"
        obj1.myDefaultMethod(); // 输出 "这是一个默认方法"

        MyOtherClass obj2 = new MyOtherClass();
        obj2.myDefaultMethod(); // 输出 "这是一个默认方法"
    }
}

In this example, MyClassthe class implements myMethod()the method, but MyOtherClassthe class does not implement myMethod()the method, so it will use the default myDefaultMethod()method. In this way, it is possible to avoid implementing the same method in each implementation class, which improves the reusability and maintainability of the code.

Guess you like

Origin blog.csdn.net/m0_64880608/article/details/131745671
Recommended