OnJava8-Examples/interfaces/nesting/NestingInterfaces.java

102 lines
1.9 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: interfaces/nesting/NestingInterfaces.java
package interfaces.nesting;
class A {
interface B {
void f();
}
public class BImp implements B {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void f() {}
}
private class BImp2 implements B {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void f() {}
}
public interface C {
void f();
}
class CImp implements C {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void f() {}
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
private class CImp2 implements C {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void f() {}
}
private interface D {
void f();
}
private class DImp implements D {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void f() {}
}
public class DImp2 implements D {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void f() {}
}
public D getD() { return new DImp2(); }
private D dRef;
public void receiveD(D d) {
dRef = d;
dRef.f();
}
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
interface E {
interface G {
void f();
}
// Redundant "public":
public interface H {
void f();
}
void g();
// Cannot be private within an interface:
//! private interface I {}
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
public class NestingInterfaces {
public class BImp implements A.B {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void f() {}
}
class CImp implements A.C {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void f() {}
}
// Cannot implement a private interface except
// within that interface's defining class:
//! class DImp implements A.D {
//! public void f() {}
//! }
class EImp implements E {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void g() {}
}
class EGImp implements E.G {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void f() {}
}
class EImp2 implements E {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void g() {}
class EG implements E.G {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void f() {}
}
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
public static void main(String[] args) {
A a = new A();
// Can't access A.D:
//! A.D ad = a.getD();
// Doesn't return anything but A.D:
//! A.DImp2 di2 = a.getD();
// Cannot access a member of the interface:
//! a.getD().f();
// Only another A can do anything with getD():
A a2 = new A();
a2.receiveD(a.getD());
}
} ///:~