- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we extend interfaces in Java? Explain?
An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.
Just like classes you can extend one interface from another using the extends keyword as shown below −
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); }
But, when you implement the sub-class you need to provide body for the abstract methods in both interfaces.
Example
In the following example we have created two interfaces − ArithmeticCalculations with two abstract methods (addition and subtraction) and, MathCalculations where,
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)); } }
Output
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
Extending multiple interfaces
In the same way you can extend multiple interfaces from an interface using the extends keyword, by separating the interfaces using comma (,) as −
interface MyInterface extends ArithmeticCalculations, MathCalculations{
Example
Following is the Java program demonstrating, how to extend multiple interfaces from a single interface.
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(); } }
Output
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
- Related Articles
- Can an interface in Java extend multiple interfaces?
- Can an interface extend multiple interfaces in Java?\n
- How to extend Interfaces in Java
- Can we extend an enum in Java?
- How can we use lambda expressions with functional interfaces in Java?
- Why do we use interfaces in Java?
- Can Enum extend any class in java?
- How we can extend multiple Python classes in inheritance?
- Can interfaces have constructors in Java?
- Can interfaces have Static methods in Java?
- Can you extend a static inner class in Java?
- Interfaces in Java
- What happens if we try to extend a final class in java?
- Where do we use $.extend() method in jQuery?
- Functional Interfaces in Java Programming

Advertisements