Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Nested interfaces

An interface defined inside another interface or a class is known as a nested interface.

By default interface is public when it is defined inside another interface.
Java doesn’t allow using private or protected access modifiers with interface when it is nested inside another interface.
Java allows using any access modifier with interface when it is nested inside another class.

To access the nested interface outside the interface or a class, it must be used with a name of the interface or a class in which it is nested.

It’s like using child name along with its parent’s name to recognize to whom does child belongs.

Example

class SoftwareCompany1 
{
    public interface CustomerDemand
    {
        public void makeSoftware();
        public void provideService();
    }
}

class SoftwareCompany2 implements SoftwareCompany1.CustomerDemand 
{
    public void makeSoftware()
    {
        System.out.println("Software Developed in Java");	
    }

    public void provideService() 
    {
        System.out.println("Will provide service for 10 years");
    }
}

class NestedInterface 
{
    public static void main(String args[])
    {
        SoftwareCompany1.CustomerDemand d1 = new SoftwareCompany2();
        d1.makeSoftware();
        d1.provideService();
    }
}

Output

Software Developed in Java
Will provide service for 10 years

Here we have used interface name CustomerDemand along with class SoftwareCompany1 because it helps the class SoftwareCompany2 to know to whom interface CustomerDemand belongs.

 

Leave a reply