Non-public top level class in Java

前端 未结 5 401
自闭症患者
自闭症患者 2021-01-03 03:28

What\'s the reason of making top-level class non-public in Java?

Let\'s say we have Foo.java, there could be

class Foo {
}
5条回答
  •  生来不讨喜
    2021-01-03 04:13

    Here is an example. No one needs to know about existence of our ConcreteDocument.

    DocumentIF.java

    public interface DocumentIF {
    }
    

    ConcreteDocument.java

    class ConcreteDocument implements DocumentIF {
    }
    

    DocumentFactory.java

    public class DocumentFactory {
        public DocumentIF createDocument() {
            return new ConcreteDocument();
        }
    }
    

提交回复
热议问题