
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Declare an Interface Within Another Interface in Java
An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.
To create an object of this type you need to implement this interface, provide body for all the abstract methods of the interface and obtain the object of the implementing class.
Nested Interfaces
Java allows declaring interfaces within another interface, these are known as nested interfaces.
While implementing you need to refer to the nested interface as outerInterface.nestedInterface.
Example
In the following Java example, we have an interface with name Cars4U_Services which contains two nested interfaces: CarRentalServices and, CarSales with two abstract methods each.
From a class we are implementing the two nested interfaces and providing body for all the four abstract methods.
interface Cars4U_Services { interface CarRentalServices { public abstract void lendCar(); public abstract void collectCar(); } interface CarSales{ public abstract void buyOldCars(); public abstract void sellOldCars(); } } public class Cars4U implements Cars4U_Services.CarRentalServices, Cars4U_Services.CarSales { public void buyOldCars() { System.out.println("We will buy old cars"); } public void sellOldCars() { System.out.println("We will sell old cars"); } public void lendCar() { System.out.println("We will lend cars for rent"); } public void collectCar() { System.out.println("Collect issued cars"); } public static void main(String args[]){ Cars4U obj = new Cars4U(); obj.buyOldCars(); obj.sellOldCars(); obj.lendCar(); } }
Output
We will buy old cars We will sell old cars We will lend cars for rent