我们可以在 Java 中扩展接口吗?解释一下?

java 8object oriented programmingprogramming

Java 中的接口类似于类,但它只包含抽象方法和字段,这些方法和字段是最终的和静态的。

就像类一样,您可以使用 extends 关键字从另一个接口扩展一个接口,如下所示 −

interface ArithmeticCalculations{
   public abstract int addition(int a, int b);
   public abstract int subtraction(int a, int b);
}
interface MathCalculations extends ArithmeticCalculations{
   public abstract double squareRoot(int a);
   public abstract double powerOf(int a, int b);
}

但是,当您实现子类时,您需要为两个接口中的抽象方法提供主体。

示例

在下面的例子中,我们创建了两个接口 − ArithmeticCalculations 和两个抽象方法(additionsubtraction)以及 MathCalculations,其中,

import java.util.Scanner;
interface ArithmeticCalculations{
   public abstract int addition(int a, int b);
   public abstract int subtraction(int a, int b);
}
interface MathCalculations extends ArithmeticCalculations{
   public abstract double squareRoot(int a);
   public abstract double powerOf(int a, int b);
}
public class ExtendingInterfaceExample implements MathCalculations{
   public int addition(int a, int b) {
      return a+b;
   }
   public int subtraction(int a, int b) {
      return a-b;
   }
   public double squareRoot(int a) {
      return Math.sqrt(a);
   }
   public double powerOf(int a, int b) {
      return Math.pow(a, b);
   }
   public static void main(String args[]){
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the value of a: ");
      int a = sc.nextInt();
      System.out.println("Enter the value of b: ");
      int b = sc.nextInt();
      ExtendingInterfaceExample obj = new ExtendingInterfaceExample();
      System.out.println("Result of addition: "+obj.addition(a, b));
      System.out.println("Result of subtraction: "+obj.subtraction(a, b));
      System.out.println("Square root of "+a+" is: "+obj.squareRoot(a));
      System.out.println(a+"^"+b+" value is: "+obj.powerOf(a, b));
   }
}

输出

Enter the value of a:
4
Enter the value of b:
3
Result of addition: 7
Result of subtraction: 1
Square root of 4 is: 2.0
4^3 value is: 64.0

扩展多个接口

同样,您可以使用 extends 关键字从一个接口扩展多个接口,方法是使用逗号 (,) 分隔接口,例如 −

interface MyInterface extends ArithmeticCalculations, MathCalculations{

示例

以下是 Java 程序,演示如何从一个接口扩展多个接口。

interface ArithmeticCalculations{
   public abstract int addition(int a, int b);
   public abstract int subtraction(int a, int b);
}
interface MathCalculations {
   public abstract double squareRoot(int a);
   public abstract double powerOf(int a, int b);
}
interface MyInterface extends MathCalculations, ArithmeticCalculations {
   public void displayResults();
}
public class ExtendingInterfaceExample implements MyInterface {
   public int addition(int a, int b) {
      return a+b;
   }
   public int subtraction(int a, int b) {
      return a-b;
   }
   public double squareRoot(int a) {
      return Math.sqrt(a);
   }
   public double powerOf(int a, int b) {
      return Math.pow(a, b);
   }
   public void displayResults(){
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the value of a: ");
      int a = sc.nextInt();
      System.out.println("Enter the value of b: ");
      int b = sc.nextInt();
      ExtendingInterfaceExample obj = new ExtendingInterfaceExample();
      System.out.println("Result of addition: "+obj.addition(a, b));
      System.out.println("Result of subtraction: "+obj.subtraction(a, b));
      System.out.println("Square root of "+a+" is: "+obj.squareRoot(a));
      System.out.println(a+"^"+b+" value is: "+obj.powerOf(a, b));
   }
   public static void main(String args[]){
      new ExtendingInterfaceExample().displayResults();
   }
}

输出

Enter the value of a:
4
Enter the value of b:
3
Result of addition: 7
Result of subtraction: 1
Square root of 4 is: 2.0
4^3 value is: 64.0

相关文章