Google+

217. Implementing the Nested Interfaces








An Interface which is declared within another interface or Class is known as Nested Interface.

Nested Interface Rules -

  • Nested Interface must be defined as public access specifier, if it is declared inside another interface
  • Nested Interface can be defined with any access specifier, if it is declared inside any Class
  • Nested Interfaces are static by default. No need of specifying as static as they are static implicitly.

Syntax - Nested interface within the interface

interface Interface_name
{
        public interface Nested_interface_name
        {

         }
}

Syntax - Nested interface within the Class

class Class_name
{
         interface Nested_interface_name
        {

         }
}

Implementing the nested interface within an interface

class ClassOne implements Interface_name.Nested_interface_name
{
       //Implementing the unimplemented methods of Nested interface here
}

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Project 'Project 44' as shown below -


2. Create an interface 'Interface1' as shown below -


3. Create a nested interface 'Interface2' inside the Interface1 as shown below -



Now lets change the public access specifier of the nested interface and see if we are getting any errors by following the below steps.

4. Change the public access specifier of Interface2 to private access specifier and observe that an error is displayed as shown below -


5. View the error message an shown below -


6. Change the access specifier of nested interface 'Interface2' and view that the error is resolved as shown below -


7. Define a method without body in the nested interface 'Interface2' as shown below -


Now lets implement the above created nested interface 'Interface2' in a Class say 'ClassOne' by following the below steps.

8. Create a Class 'ClassOne' as shown below -


9. Implement the nested interface 'Interface2' in the Class 'ClassOne' as shown below -


10. Implement the unimplemented methods of nested interface 'Interface2' in the 'ClassOne' class to resolve the above error as shown below -


Now lets access the implemented method 'method2' in 'ClassOne' using the Interface1.Interface2 object (i.e. Upcasting the ClassOne class object to Interface1.Interface2  nested interface object) by following the below steps.

11. Create a Class 'ClassA' with main( ) method as shown below -



12. In order to access the interface2 method that is implemented in the ClassOne class using the nested interface object, we have to upcast the ClassOne object reference to Interface2 object as shown below -


13. Now access the method 'method2' of interface2 which is implemented in ClassOne class by using the interface object as shown below -



14. Run the Java Class file 'ClassA' and observe that the message inside the println statement of method2 method in ClassOne class is printed in the Eclipse IDE -> Console as output as shown below -


Hence we have implemented the nested interface within in an interface. Now lets implement the nested interface within a Class.

Implementing the nested interface within a Class

class ClassOne implements Class_name.Nested_interface_name
{
       //Implementing the unimplemented methods of Nested interface here
}

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Project 'Project 45' as shown below -


2. Create a Class say 'Class1' as shown below -


3. Create a nested interface 'Interface1' inside the Class1 class as shown below -


Now lets change the access specifier of the nested interface 'Interface1' within Class1 class from public to protected and find out that no errors are displayed by following the below steps.

4. Change the access specifier of nested interface inside Class from public to protected and observe that no errors are displayed as the interface is within the Class as shown below - (We would have got the errors if the nested interface is within another interface instead of Class)



5. Define a method 'method1' without body inside the nested interface 'Interface1' as shown below -


Lets implement the method  'method1' of  nested interface 'Interface1' within the Class1

6. Create another Class 'ClassX' as shown below -


7. Implement the nested interface 'Interface1' in the ClassX as shown below -


8. Implement the unimplemented method 'method1' of  nested interface 'Interface1' and observe that the error got resolved as shown below -


Now lets access the method1 of nested interface which is implemented in ClassX. In order to access the implemented method of nested interface in ClassX using nested interface object, we have to upcast the ClassX object to the nested interface object.

9. Create a Class 'ClassZ' as shown below -



10. Upcast the ClassX object to nested inteface 'interface1' object as shown below -


11. Access the interface implemented method method1 in ClassX using the nested interface object as shown below -



12. Run the Java Class file 'ClassZ' and observe that the method1 got accessed by interface1 object and the print statement in the method1 got printed in the Eclipse -> Console as output as shown below -



This is how the nested interface is created within an Interface and Class.






Please comment below to feedback or ask questions.

Exception is an Object will be explained in the next post.





No comments:

Followers :)

Blog Index