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); } उसी तरह आप extends कीवर्ड का उपयोग करके एक इंटरफेस से कई इंटरफेस को विस्तारित कर सकते हैं, इंटरफेस को कॉमा (,) से अलग करके -
interface MyInterface extends ArithmeticCalculations, 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 { 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