Non public top-level classes in Java

What is the reason for creating non-public top-level classes in Java?

Suppose we have foo Java, there may be

class Foo {
}

or

public class Foo {
}

I know that the previous example will have some class visibility problems (which may not be seen in other packages) But anyway, is there any reason why someone might do what they did in the first code example?

UPD: what shortcomings did I see in the previous solution: no one cares that it is private You can simply extend the class later through other public classes in the same package, and then the non-public part of the class may cause you visibility / access problems

Solution

This is an example

DocumentIF. java

public interface DocumentIF {
}

ConcreteDocument. java

class ConcreteDocument implements DocumentIF {
}

DocumentFactory. java

public class DocumentFactory {
    public DocumentIF createDocument() {
        return new ConcreteDocument();
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>