Guide for using new features of interfaces in Java8

  • 2021-12-05 06:18:26
  • OfStack

Directory preface compiles test class ComepareTest in the first case, a class implements an interface and inherits a parent class at the same time. Case 2, Case 3 summary

Preface

In Java8, there are no longer only abstract methods in the interface, but also static methods and default methods. At this time, the interface is more like a class. Let's see how to use it ~

In Java8, static and default methods can be added to interfaces.

Static method: Decorate with static keyword. You can call static methods directly through the interface and execute their method bodies

Default: Decorated with the default keyword. You can call the

Just look at the code


package com.nanfeng.demo.interfacepractice.java8;
 
/**
 * java8 New features in 
 *  The permissions of the default method in the interface are public, So public It can also be omitted 
 */
public interface CompareA {
    //  You can define abstract methods in the interface 
    public static void method1() {
        System.out.println("Java8 You can define static methods in the interface and call them through the interface --1");
    }
 
    //  Default method 
    public default void method2() {
        System.out.println("Java8 Default methods can be defined in the interface in --2");
    }
    public default void method3() {
        System.out.println("Java8 Default methods can be defined in the interface in --3");
    }
}
 

Writing Test Class ComepareTest


package com.nanfeng.demo.interfacepractice.java8;
 
public class CompareATest {
    public static void main(String[] args) {
        //  Creating an Implementation Class Object 
        CompareAClass c = new CompareAClass();
        /**
         *  Knowledge point 1 Static methods in interfaces   Can only be called through an interface 
         * Static method may be invoked on containing interface class only
         *  Static methods can only be called when an interface class is included          */
        // c.method1();
 
        //  Calling using an interface method1() Method, the interface at this time is a bit like a tool class 
        CompareA.method1();
        /**
         *  Knowledge point 2 : 
         *  Default method, which can be called in the interface by creating an object that implements the class 
         *  Or you can override the default method in the docking port 
         */
        c.method2();
    }
}
 
class CompareAClass implements CompareA {
    /**
     *  When overriding the default method in the docking port in the implementation class, 
     *  Note: It cannot be omitted public Permission modification, otherwise an error will be reported 
     *  When executed, the method we override will still be called, which is consistent with inheritance 
     *
     *
     */
    @Override
    public void method2() {
        System.out.println("Java8 The implementation class in can override the default method in the interface. Note that the permission modifier of the declared method is public, And public Can not be omitted ");
    }
}

Run results:

Static methods can be defined in the interface in Java8, which can be called through the interface
The implementation class in Java8 can override the default method in the interface. Note that the permission modifier of the declared method is public, and public cannot be omitted--2

In the first case, a class implements an interface and inherits a parent class at the same time

1. Create the parent class of the implementation class


package com.nanfeng.demo.interfacepractice.java8;
 
/**
 *  Implement the parent class of the class 
 */
public class SuperClass {
    //  Define a method with the same name as the interface 
    public void method3(){
        System.out.println(" In the parent class of the implementation class, methods with the same name and parameters as those in the interface appear --SuperClass");
    }
}

2. Let the subclass inherit the parent class while implementing the interface


package com.nanfeng.demo.interfacepractice.java8;
 
public class CompareATest {
    public static void main(String[] args) {
        //  Creating an Implementation Class Object 
        CompareAClass c = new CompareAClass();
        /**
         *  Knowledge point 1 Static methods in interfaces   Can only be called through an interface 
         * Static method may be invoked on containing interface class only
         *  Static methods can only be called when an interface class is included          */
        // c.method1();
 
        //  Calling using an interface method1() Method, the interface at this time is a bit like a tool class 
        CompareA.method1();
        /**
         *  Knowledge point 2 : 
         *  Default method, which can be called in the interface by creating an object that implements the class 
         *  Or you can override the default method in the docking port 
         */
        c.method2();
         /**
         *  Knowledge point 3 : 
         *  What should I do when the method in the parent class and the method in the interface have the same name? 
         *  If a subclass (or implementation class) inherits a parent class and implements an interface that declares a method with the same name and parameter 
         *  Then when the subclass does not override this method, the default call is the method with the same name and parameter in the parent class. 
         * --> Class priority principle 
         */
        c.method3();
    }
}
 
class ComepareAClass extends SuperClass implements CompareA {
    /**
     *  When overriding the default method in the docking port in the implementation class, 
     *  Note: It cannot be omitted public Permission modification, otherwise an error will be reported 
     *  When executed, the method we override will still be called, which is consistent with inheritance 
     */
    @Override
    public void method2() {
        System.out.println("Java8 The implementation class in can override the default method in the interface. Note that the permission modifier of the declared method is public, And public Can not be omitted ");
    }
 
 
}

Run View Results:

Static methods can be defined in the interface in Java8 and invoked through the interface--1
The implementation class in Java8 can override the default method in the interface. Note that the permission modifier of the declared method is public, and public cannot be omitted--2
In the parent class of the implementation class, a method with the same name as that in the interface appears-SuperClass

Situation 2

How should the implementation class implement multiple interfaces without inheriting the parent class

1. Create the CompareB interface, and create the default method of the same parameter as the CompareA interface

Note: If a class inherits multiple interfaces at the same time, interface conflicts will occur when default methods with the same name and parameters appear in the interfaces. At this point, the implementation class must override this method


/**
 * Java Multiple inheritance of interfaces is supported in interfaces 
 *  Situation 1 : 
 * class ComepareAClass extends SuperClass implements CompareA
 * CompareA Interface and SuperClass When a method with the same name and parameter appears in the parent class, the method in the parent class will be called by default, which embodies the principle of class priority 
 *  Situation 2 : 
 * class ComepareAClass implements CompareA, CompareB
 *  Without inheriting the parent class, 1 Under the premise that a class inherits from multiple interfaces, the default methods with the same name and parameters are defined in the two interfaces. How will they be executed? 
 *  Knowledge point 4 : 
 *  If 1 If the implementation class implements multiple interfaces and the default method with the same name and the same parameter is defined in these interfaces, an error will be reported if the implementation class does not override this method 
 * --> There will be an excuse conflict 
 *  This requires us to override this method in the implementation class 
 *
 */
class CompareAClass implements CompareA, CompareB {
    /**
     *  When overriding the default method in the docking port in the implementation class, 
     *  Note: It cannot be omitted public Permission modification, otherwise an error will be reported 
     *  When executed, the method we override will still be called, which is consistent with inheritance 
     */
    @Override
    public void method2() {
        System.out.println("Java8 The implementation class in can override the default method in the interface. Note that the permission modifier of the declared method is public, And public Can not be omitted --2");
    }
    //  To resolve interface conflicts, the methods in the interface must be rewritten 
    @Override
    public void method3() {
        System.out.println("ComepareAClass The implementation class overrides the method of the same name in multiple interfaces, and the rewritten method executes the method in the implementation class --method3()");
    }
}

Run results:

Static methods can be defined in the interface in Java8, which can be invoked through the interface--1
The implementation class in Java8 can override the default method in the interface. Note that the permission modifier of the declared method is public, and public cannot be omitted--2
The ComepareAClass implementation class rewrites the method of the same name in multiple interfaces, and the rewritten method executes the method in the implementation class-method3 ()

Situation 3

In self-defined methods in subclasses (or implementation classes), call interfaces and methods in parent classes that are not overridden


/**
 * Java Multiple inheritance of interfaces is supported in interfaces 
 *  Situation 1 : 
 * class ComepareAClass extends SuperClass implements CompareA
 * CompareA Interface and SuperClass When a method with the same name and parameter appears in the parent class, the method in the parent class will be called by default, which embodies the principle of class priority 
 *  Situation 2 : 
 * class ComepareAClass implements CompareA, CompareB
 *  Without inheriting the parent class, 1 Under the premise that a class inherits from multiple interfaces, the default methods with the same name and parameters are defined in the two interfaces. How will they be executed? 
 *  Knowledge point 4 : 
 *  If 1 If the implementation class implements multiple interfaces and the default method with the same name and the same parameter is defined in these interfaces, an error will be reported if the implementation class does not override this method 
 * --> There will be an excuse conflict 
 *  This requires us to override this method in the implementation class 
 *  Situation 3 : 
 * class CompareAClass extends SuperClass implements CompareA, CompareB
 * 1 A subclass (or implementation class) inherits the parent class and implements multiple interfaces at the same time 
 *  In self-defined methods in subclasses (or implementation classes), call interfaces and methods in parent classes that are not overridden 
 * */
class CompareAClass extends SuperClass implements CompareA, CompareB {
    /**
     *  When overriding the default method in the docking port in the implementation class, 
     *  Note: It cannot be omitted public Permission modification, otherwise an error will be reported 
     *  When executed, the method we override will still be called, which is consistent with inheritance 
     */
    @Override
    public void method2() {
        System.out.println("Java8 The implementation class in can override the default method in the interface. Note that the permission modifier of the declared method is public, And public Can not be omitted --2");
    }
    //  To resolve interface conflicts, the methods in the interface must be rewritten 
    @Override
    public void method3() {
        System.out.println("ComepareAClass The implementation class overrides the method of the same name in multiple interfaces, and the rewritten method executes the method in the implementation class --method3()");
    }
 
    /**
     *  Knowledge point 5 How to call a method of a parent class (or interface) that is not overridden in a method of a subclass (or implementation class) 
     */
    public void myMethod(){
        //  Call the self-overridden method3 () method 
        this.method3();
        //  Object declared in the parent class method3() Method 
        super.method3();
        //  Default method in tuning interface ( Note: It is a non-static method, so you cannot call it with the interface name )
        // Call mode: Interface name .super. Method 
        CompareA.super.method3();
        CompareB.super.method3();
 
    }
}

Summarize


Related articles: