nesting interfaces

knowledge:

1. interfaces can also be private. (see class A)

2. implementing a private interface is a way to force the definition of the methods in that interface without adding any type information (that is, without allowing any upcasting). (see class A)

3. interfaces can be nested within each other. nested interface can not be private. (see interface E)

4. notice that when we implement an interface, we are not required to implement any interfaces nested within. Also, private interfaces cannot be implemented outside of their defining classes.

for example:

// interfaces/nesting/NestingInterfaces.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {java interfaces.nesting.NestingInterfaces}
package interfaces.nesting;

class A {
  interface B {
    void f();
  }

  public class BImp implements B {
    @Override
    public void f() {}
  }

  private class BImp2 implements B {
    @Override
    public void f() {}
  }

  public interface C {
    void f();
  }

  class CImp implements C {
    @Override
    public void f() {}
  }

  private class CImp2 implements C {
    @Override
    public void f() {}
  }

  private interface D {
    void f();
  }

  private class DImp implements D {
    @Override
    public void f() {}
  }

  public class DImp2 implements D {
    @Override
    public void f() {}
  }

  public D getD() {
    return new DImp2();
  }

  private D dRef;

  public void receiveD(D d) {
    dRef = d;
    dRef.f();
  }
}

interface E {
  interface G {
    void f();
  }
  // Redundant "public":
  public interface H {
    void f();
  }

  void g();
  // Cannot be private within an interface:
  // - private interface I {} // error: illegal combination of modifiers: public and private
  private interface I {}
}

public class NestingInterfaces {
  public class BImp implements A.B {
    @Override
    public void f() {}
  }

  class CImp implements A.C {
    @Override
    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 {
    @Override
    public void g() {}
  }

  class EGImp implements E.G {
    @Override
    public void f() {}
  }

  class EImp2 implements E {
    @Override
    public void g() {}

    class EG implements E.G {
      @Override
      public void f() {}
    }
  }

  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());
  }
}

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/interfaces/nesting/NestingInterfaces.java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值