
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to inherit multiple interfaces in Java?
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. You can also 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
- How multiple inheritance is implemented using interfaces in Java?
- Can an interface in Java extend multiple interfaces?
- Can an interface extend multiple interfaces in Java?\n
- How to extend Interfaces in Java
- Interfaces in Java
- How to inherit a class in C#?
- Can we inherit a final method in Java?
- How abstraction is achieved using interfaces in Java?
- Functional Interfaces in Java Programming
- Evolution of interfaces in Java
- C# Equivalent to Java Functional Interfaces
- How to use Consumer and BiConsumer interfaces in lambda expression in Java?
- How to use Function and BiFunction interfaces in lambda expression in Java?
- Interfaces and inheritance in Java Programming
- Can interfaces have constructors in Java?

Advertisements