Tuesday, 17 October 2017

7 Nested interfaces

1. Nested interface
  1. An interface i.e. declared within another interface or class is known as nested interface.
  2. The nested interfaces are used to group related interfaces so that they can be easy to maintain.
  3. The nested interface must be referred by the outer interface or class. It can't be accessed directly.
  4. Nested interfaces are declared static implicitly.
interface interface_name{  
...  
interface nested_interface_name{  
 ...  
}  
}   
class class_name{  
...  
interface nested_interface_name{  
 ...  
}  
}   
  1. We can’t declare interface in a block or a method.
  2. We can also declare a class inside a interface, it is static nested class
package com.nested;

public class Main {
public static void main(String[] args) {
Main.Test m1 = new Main.StaticTestImpl();
Main.Test m2 = new Main().new TestImpl();
m1.print();
m2.print();
}
interface Test{
int a =1;
void print();
class MyTest{
}
}
static class StaticTestImpl implements Main.Test{
@Override
public void print() {
System.out.println(this+"  "+a);
abstract class AbstractTest{
abstract void print();
}
}
}
class TestImpl implements Main.Test{
@Override
public void print() {
System.out.println(this);
}
}
}


No comments:

Post a Comment

3. Java Program to create Binary Tree