Java 7、8、9 中接口概念的变化

Interfaces till Java 7

In Java SE 7 or earlier versions, an interface can have only two things i.e. Constant variables and Abstract methods. These interface methods MUST be implemented by classes which choose to implement the interface.

翻译

在 jdk 7 或更早版本中,接口里面只能有常量变量和抽象方法。这些接口方法必须由选择实现接口的类实现。

Example

// Java 7 program to illustrate 
// private methods in interfaces 
public interface TempI { 
    public abstract void method(int n); 
} 
  
class Temp implements TempI { 
    @Override
    public void method(int n) 
    { 
        if (n % 2 == 0) 
            System.out.println("geeksforgeeks"); 
        else
            System.out.println("GEEKSFORGEEKS"); 
    } 
  
    public static void main(String[] args) 
    { 
        TempI in1 = new Temp(); 
        TempI in2 = new Temp(); 
        in1.method(4); 
        in2.method(3); 
    } 
} 
OUTPUT : geeksforgeeks
         GEEKSFORGEEKS

Java 8 Interface Changes

Some new features to interface were introduced in Java 8 i.e. Default methods and Static methods feature. In Java 8, an interface can have only four types:

简单翻译

jdk8 的时候接口可以有默认方法和静态方法功能

  1. Constant variables

  2. Abstract methods

  3. Default methods

  4. Static methods

Example


// Java 8 program to illustrate 
// static, default and abstract methods in interfaces 
public interface TempI { 
  
    public abstract void div(int a, int b); 
  
public default void
    add(int a, int b) 
    { 
        System.out.print("Answer by Default method = "); 
        System.out.println(a + b); 
    } 
  
    public static void mul(int a, int b) 
    { 
        System.out.print("Answer by Static method = "); 
        System.out.println(a * b); 
    } 
} 
  
class Temp implements TempI { 
  
    @Override
    public void div(int a, int b) 
    { 
        System.out.print("Answer by Abstract method = "); 
        System.out.println(a / b); 
    } 
  
    public static void main(String[] args) 
    { 
        TempI in = new Temp(); 
        in.div(8, 2); 
        in.add(3, 1); 
        TempI.mul(4, 1); 
    } 
} 

OUTPUT : Answer by Abstract method = 4
         Answer by Default method = 4
         Answer by Static method = 4

Java 9 Interface Changes

Java 9 introduced private methods and private static method in interfaces. In Java 9 and later versions, an interface can have six different things:

简单翻译

Jdk 9 在接口中引入了私有方法和私有静态方法。

  1. Constant variables

  2. Abstract methods

  3. Default methods

  4. Static methods

  5. Private methods

  6. Private Static methods

These private methods will improve code re-usability inside interfaces and will provide choice to expose only our intended methods implementations to users.These methods are only accessible within that interface only and cannot be accessed or inherited from an interface to another interface or class.


// Java 9 program to illustrate 
// private methods in interfaces 
public interface TempI { 
  
    public abstract void mul(int a, int b); 
  
public default void
    add(int a, int b) 
    { 
// private method inside default method 
        sub(a, b);  
  
 // static method inside other non-static method 
        div(a, b); 
        System.out.print("Answer by Default method = "); 
        System.out.println(a + b); 
    } 
  
    public static void mod(int a, int b) 
    { 
        div(a, b); // static method inside other static method 
        System.out.print("Answer by Static method = "); 
        System.out.println(a % b); 
    } 
  
    private void sub(int a, int b) 
    { 
        System.out.print("Answer by Private method = "); 
        System.out.println(a - b); 
    } 
  
    private static void div(int a, int b) 
    { 
        System.out.print("Answer by Private static method = "); 
        System.out.println(a / b); 
    } 
} 
  
class Temp implements TempI { 
  
    @Override
    public void mul(int a, int b) 
    { 
        System.out.print("Answer by Abstract method = "); 
        System.out.println(a * b); 
    } 
  
    public static void main(String[] args) 
    { 
        TempI in = new Temp(); 
        in.mul(2, 3); 
        in.add(6, 2); 
        TempI.mod(5, 3); 
    } 
}

OUTPUT : Answer by Abstract method = 6              // mul(2, 3) = 2*3 = 6
         Answer by Private method = 4               // sub(6, 2) = 6-2 = 4 
         Answer by Private static method = 3        // div(6, 2) = 6/2 = 3
         Answer by Default method = 8               // add(6, 2) = 6+2 = 8
         Answer by Private static method = 1        // div(5, 3) = 5/3 = 1 
         Answer by Static method = 2                // mod(5, 3) = 5%3 = 2

参考

  • https://www.geeksforgeeks.org/private-methods-java-9-interfaces/
上次更新: 2020-5-13 10:08:26 AM