Why should we declare an interface inside a class?

后端 未结 5 1341
遥遥无期
遥遥无期 2020-12-02 13:30

Why should we declare an interface inside a class in Java?

For example:

public class GenericModelLinker implements IModelLinker {

  private static f         


        
5条回答
  •  遥遥无期
    2020-12-02 13:52

    Inside your class you may need multiple implementations of an interface, which is only relevant to this particular class. In that case make it an inner interface, rather than a public / package-private one.
    Only an interface inside a class can be declared private or protected. Sometimes, that makes sense, when the interface is only appropriate for use inside the outer class (or its subclasses).

提交回复
热议问题