beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Nested or Inner interfaces in Java

By Chaitanya Singh | Filed Under: OOPs Concept

An interface which is declared inside another interface or class is called nested interface. They are also known as inner interface. Since nested interface cannot be accessed directly, the main purpose of using them is to resolve the namespace by grouping related interfaces (or related interface and class) together.  This way, we can only call the nested interface by using outer class or outer interface name followed by dot( . ), followed by the interface name.

Example: Entry interface inside Map interface is nested. Thus we access it by calling Map.Entry.

Note:

  • Nested interfaces are static by default. You don’t have to mark them static explicitly as it would be redundant.
  • Nested interfaces declared inside class can take any access modifier, however nested interface declared inside interface is public implicitly.

Example 1: Nested interface declared inside another interface

interface MyInterfaceA{  
    void display();  
    interface MyInterfaceB{  
        void myMethod();  
    }  
}  
      
class NestedInterfaceDemo1 
    implements MyInterfaceA.MyInterfaceB{  
     public void myMethod(){
         System.out.println("Nested interface method");
     }  
      
     public static void main(String args[]){  
         MyInterfaceA.MyInterfaceB obj=
                 new NestedInterfaceDemo1(); 
      obj.myMethod();  
     }  
}

Output:
Nested interface method

Example 2: Nested interface declared inside a class

class MyClass{  
    interface MyInterfaceB{  
        void myMethod();  
    }
}  
    
class NestedInterfaceDemo2 implements MyClass.MyInterfaceB{  
     public void myMethod(){
         System.out.println("Nested interface method");
     }  
    
     public static void main(String args[]){  
        MyClass.MyInterfaceB obj=
               new NestedInterfaceDemo2();  
        obj.myMethod();  
     }  
}

Output:
Nested interface method

Enjoyed this post? Try these related posts

  1. Java static constructor – Is it really Possible to have them in Java?
  2. Garbage Collection in Java
  3. OOPs concepts – What is Aggregation in java?
  4. Packages in java and how to use them
  5. Abstract method in Java with examples
  6. Constructor Overloading in Java with examples

Comments

  1. Ram Kumar Shrestha says

    March 12, 2017 at 3:18 AM

    In Example 1 of Nested Interface you are creating an object by referencing the inner interface via interface
    //MyInterfaceA.MyInterfaceB = new NestedInterfaceDemo1();

    What is the difference or advantage of doing so instead of referencing by
    class name //InterfaceDemo obj = new Nested InterfaceDemo1();

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Index
  • Java Introduction
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Control Statements

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor
  • Java Interview Q

MORE ...

  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap