![]() |
![]() |
Java Interface | |
Java Class vs Interface | |
Java Multiple Inheritance | |
Java Nested Interface |
Java Packages | |
Java Built in packages | |
Java Package Access Specifiers |
Introduction of Errors | |
Java Exceptions | |
Java Try Block | |
Java Catch Block | |
Java Finally Block | |
Java Throw Keyword | |
Java Throws Keyword | |
Java Builtin Exceptions | |
User Defined Exceptions |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
Nesting means writting one thing into the another one. Interface can be declared inside the class or in another interface.
The nesting of interface leads to the easy maintaining and accessing the group of related interfaces.
An interface can be declared inside a class body or interface body is called as nesting of interfaces.Nesting of interface sometimes called as interface within interface or interface within class or inner interface.
Note : Nested interface cannot be accessed directly. To access nested interface, it must be reffered by the outer interface or class.
interface
.The interface can be nested within another interface.
interface interfaceName { // outer interface declarations ... ... interface Nested_interfaceName { // inner interface declarations ... ... } }
Example to demonstrate nested interface within interface.
interface Book { interface Java { void displayJava(); } interface Python { void displayPython(); } } class Student implements Book.Java, Book.Python { public void displayPython() { System.out.println("Python Book available"); } public void displayJava() { System.out.println("Java Book available"); } public static void main(String args[]) { Student stud = new Student(); stud.displayJava(); stud.displayPython(); } }
Output
In above example we have declared Book as outer interface. Interface Java and Python are nested inside the Book interface.
How to implement? - For implementing nested interface we must specify outer interface name first then dot then inner interface name.
Example : Book.Java
and Book.Python
The second way of nesting is an interface inside the class.Just like first type we can write nesting of interface.
class className { // body of class ... ... interface inner_interfaceName { // declarations of interface ... ... } }
Example to demostrate nesting of interface within class.
class Earth { String satelliteName = "Moon"; double gravity = 9.807; byte maxTemperature = 58; void showEarth() { System.out.println("Earth's Natural Satellite : "+satelliteName); System.out.println("Gravity : "+gravity); System.out.println("Max Temperature : "+maxTemperature); } interface Human { String species = "Homo saplens"; String types = "Male and Female"; void displayInfo(); } } class GetInfo extends Earth implements Earth.Human { public void displayInfo() { System.out.println("\nHuman species : "+species); System.out.println("Human types : "+types); } public static void main(String args[]) { GetInfo get = new GetInfo(); get.showEarth(); get.displayInfo(); } }
Output
In above example Earth is the outer class and Human is the interface. We have written showEarth() method for class Earth which is called by extending it to the class GetInfo. We have also nested one interface called Human which contains the method called displayInfo().