New features in JDK8 – Interface enhanced content

The interface first

Interface content prior to JDK8

interfaceThe interface name{static constant; Abstract method; }Copy the code

Updated interface enhancements for JDK8

interfaceThe interface name{static constant; Abstract method; Static method; Default method}Copy the code

JDK8 enhances the default and static methods of interfaces. What are the default and static methods? Now, let’s talk about it

Before we explore, let’s take a look at the interface before JDK8 and some of its features

We started by defining interface A and then defined its two implementation classes, B and C. The implementation class overrides the test1 methods defined in interface A and then calls the methods in the interface through the implementation class

package com.features.Interface;

public class demo01 {
    public static void main(String[] args) {
        A b = new B();
        A c = new C();
        // Initial method callb.test1(); c.test1(); }}interface A{
    void test1(a);
}

class B implements A{

    @Override
    public void test1(a) {
        System.out.println("Method test1 in B"); }}class C implements A{
    @Override
    public void test1(a) {
        System.out.println("Method test1 in C"); }}Copy the code

At this point, we have A business requirement to add A test2 method to interface A. We need to add test2 method to interface A, and then its implementation class also needs to override test2 method

package com.features.Interface;

public class demo01 {
    public static void main(String[] args) {
        A b = new B();
        A c = new C();
        // Initial method call
        b.test1();
        c.test1();
        // Business requirements, interface extension method callsb.test2(); c.test2(); }}interface A{
    void test1(a);
    The test2 method will need to be overridden in all implementation classes of interface A
    void test2(a);
}

class B implements A{

    @Override
    public void test1(a) {
        System.out.println("Method test1 in B");
    }

    @Override
    public void test2(a) {
        System.out.println("Because of business needs, the test2 method of the interface extension is overwritten by B."); }}class C implements A{
    @Override
    public void test1(a) {
        System.out.println("Method test1 in C");
    }

    @Override
    public void test2(a) {
        System.out.println("Because of business needs, the test2 method of interface extension is overwritten by C."); }}Copy the code

So, every time we need to extend the interface method, its implementation class needs to rewrite the new method, when the implementation of more than one class, we extend the interface method is very expensive, so this time, JDK8 new default method and static method to solve this problem.

Interface enhanced content – the default method

Default method:

  1. Defined in the interface,Its implementation class inherits it by default and can be overridden
  2. throughInstance name. Method nameTo invoke the

For example, we added A test3 method to interface A

package com.features.Interface;

public class demo01 {
    public static void main(String[] args) {
        A b = new B();
        A c = new C();
        // Initial method call
        b.test1();
        c.test1();
        // Business requirements, interface extension method calls
        b.test2();
        c.test2();
        // Add a default method to the interface called by instance name. The method nameb.test3(); c.test3(); }}interface A{
    void test1(a);
    The test2 method will need to be overridden in all implementation classes of interface A
    void test2(a);
    // there are default methods and static methods in the interface after JDK8
    // The default method
    // Defined in an interface whose implementation class can be inherited by default or overridden
    public default String test3(a){
        System.out.println("Default method appears in JDK8, implementation class of interface automatically inherits by default");
        return "default method"; }}class B implements A{

    @Override
    public void test1(a) {
        System.out.println("Method test1 in B");
    }

    @Override
    public void test2(a) {
        System.out.println("Because of business needs, the test2 method of the interface extension is overwritten by B.");
    }

    @Override
    public String test3(a) {
        System.out.println("B overrides the default method for interface A.");
        return "Override default method"; }}class C implements A{
    @Override
    public void test1(a) {
        System.out.println("Method test1 in C");
    }

    @Override
    public void test2(a) {
        System.out.println("Because of business needs, the test2 method of interface extension is overwritten by C."); }}Copy the code

Results:

Interface enhanced content – static methods

Static method:

  1. Defined in the interface,It cannot be inherited or overridden
  2. throughInterface name Method nameTo invoke the

For example, we added A test4 method to interface A

package com.features.Interface;

public class demo01 {
    public static void main(String[] args) {
        A b = new B();
        A c = new C();
        // Initial method call
        b.test1();
        c.test1();
        // Business requirements, interface extension method calls
        b.test2();
        c.test2();
        // Add a default method to the interface called by instance name. The method name
        b.test3();
        c.test3();
        // Add a static method to the interface. The method nameA.test4(); }}interface A{
    void test1(a);
    The test2 method will need to be overridden in all implementation classes of interface A
    void test2(a);
    // there are default methods and static methods in the interface after JDK8
    // The default method
    // Defined in an interface whose implementation class can be inherited by default or overridden
    public default String test3(a){
        System.out.println("Default method appears in JDK8, implementation class of interface automatically inherits by default");
        return "default method";
    }
    // Static method
    // Defined in the interface, it cannot be inherited or overridden
    public static String test4(a){
        System.out.println("Static methods in JDK8 that cannot be inherited or overridden");
        return "static method"; }}class B implements A{

    @Override
    public void test1(a) {
        System.out.println("Method test1 in B");
    }

    @Override
    public void test2(a) {
        System.out.println("Because of business needs, the test2 method of the interface extension is overwritten by B.");
    }

    @Override
    public String test3(a) {
        System.out.println("B overrides the default method for interface A.");
        return "Override default method"; }}class C implements A{
    @Override
    public void test1(a) {
        System.out.println("Method test1 in C");
    }

    @Override
    public void test2(a) {
        System.out.println("Because of business needs, the test2 method of interface extension is overwritten by C."); }}Copy the code

Results:

Default method vs. static method

  1. Different nature
  • The default method is inherited by its implementation class by default, and its implementation class can override it
  • A static method is not inherited by its implementing class, nor can its implementing class override it
  1. Different calls
  • The default method passesInstance name. Method namecall
  • Static method throughInterface name Method namecall