
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
Nested Interface in Java
In Java, an interface defined inside another interface or class is called nested interface. Interfaces are similar to classes, but they not contain instance variables, and their methods are declared without any body. You can specify what a class must do, but not how it does it using the interface keyword.
In this article, we will learn about nested interfaces in Java.
What is a Nested Interface in Java
As mentioned earlier, we can declare an interface within another interface or class. Such an interface is termed as nested interface. It follows the same syntax as a regular interface but is enclosed within an outer class or interface.
Follow the given guidelines while creating a nested interface:
- A nested interface declared within an interface must be public.
- A nested interface declared within a class can have any access modifier.
- It is by default static.
Syntax of Java Nested Interface
Syntax to create a nested interface within a class is as follows:
class nameOfClass { interface nameOfNestedInterface { // code } }
Syntax to create a nested interface within another interface is as follows:
class nameOfInterface { interface nameOfNestedInterface { // code } }
Uses of Java Nested Interface
You can use the Java nested interface for the following reasons:
- Nesting one interface within another helps in grouping related interfaces together.
- You can use it for code security as it limits the scope and prevent the interfaces from being accessed outside of the class in which they are defined.
- It also allow to implement multiple interfaces in a single class.
Types of Nested Interfaces
Nested interfaces in Java can be classified into two types:
- Member Interface
- Interface Inside Another Interface
Member Interface
A member interface is an interface declared inside a class. It let's us to use encapsulation and logical grouping of behavior. It can have any access modifier.
Example
Below is an example of a nested interface using the Member interface:
class Animal { interface Activity { void move(); } } class Dog implements Animal.Activity { public void move() { System.out.println("Dogs can walk and run"); } } public class Tester { public static void main(String args[]) { Dog dog = new Dog(); dog.move(); } }
Output of the above code is given below:
Dogs can walk and run
Interface Inside Another Interface
A nested interface inside another interface helps in grouping related functionalities within an outer interface. It is always public and is called using the name of the outer interface.
Example
Below is an example of an interface inside another interface:
interface OuterInterface { interface InnerInterface { void display(); } } class Test implements OuterInterface.InnerInterface { public void display() { System.out.println("Nested Interface inside another Interface"); } } public class Main { public static void main(String[] args) { OuterInterface.InnerInterface obj = new Test(); obj.display(); } }
On running the above code, you will get the following result:
Nested Interface inside another Interface