1. Nested interface
- An interface i.e. declared within another interface or class is known as nested interface.
- The nested interfaces are used to group related interfaces so that they can be easy to maintain.
- The nested interface must be referred by the outer interface or class. It can't be accessed directly.
- Nested interfaces are declared static implicitly.
interface interface_name{
...
interface nested_interface_name{
...
}
}
|
class class_name{
...
interface nested_interface_name{
...
}
}
|
- We can’t declare interface in a block or a method.
- 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