Pros and cons of package private classes in Java?

I am learning Java recently, and I came across the notion of package-private classes, which is the default if we don't specify anything. But then I realized:

  • I seldom see the use of package-private class. Is there a reason for this, eg, it has serious drawbacks, it is redundant, or simply I am not reading enough? Are there strong arguments for/against its usage?

  • If it is really not useful in most cases, why would it be the default?

  • In what situation should we use package-private in the real world? Ie, when would it become irreplaceable?

  • In other words, what are the major pros and cons of the default package-private modifier?


    The short answer is - it's a slightly wider form of private.

    I'll assume that you're familiar with the distinction between public and private , and why it's generally good practice to make methods and variables private if they're going to be used solely internally to the class in question.

    Well, as an extension to that - if you're thinking about creating your software in a modular way, you might think about a public interface to your module, which will have multiple classes inside it collaborating between themselves. In this context it makes perfect sense to make methods public if they're going to be called by consumers; private if they're internal to a class; and package private if they're used to call between classes in this module, ie it's an implementation detail of your module (as seen by public callers) but spans several classes.

    This is seldom used in practice, because the package system turns out to not be so useful for this sort of thing. You'd have to dump all of the classes for a given module into exactly the same package, which for anything non-trivial is going to get a bit unwieldy. So the idea is great - make a method accessible to just a handful of "nearby" classes, as a slightly wider private - but the restrictions on how you define that set of classes means it's rarely used/useful.


    One nice thing about package-private is that you can use it to give access to methods you would otherwise consider private to unit test classes. The downside of course being that other classes in the package could call it when they really shouldn't.


    Regarding the question of "why would it be the default,", in this context, the term "default" just means the absence of another qualifier. I guess they could have invented another keyword ("package" was already taken), but they didn't.

    In the real world, I use default access for utility classes and abstract classes that I don't want people to call or otherwise use from other packages. Let's say you have an interface and two concrete implementations that extend from some abstract class. You declare your two concrete classes as final because you don't necessarily want people to subclass them (see Effective Java). You also don't want people to monkey around with your abstract class for the same reason. If you use default access for the abstract class, then people only see it if they put their class in your package. It's not bullet proof, but I think it's a reasonable use/illustration of default access. That said, the fact that it does not prevent the details from leaking as private would, ie doesn't guarantee anything, means that it's not a particularly useful convention.

    Another reason why you haven't see it used more often is that people tend to exclude classes with default access from their javadocs.

    链接地址: http://www.djcxy.com/p/24044.html

    上一篇: Java中的默认访问修饰符是什么?

    下一篇: Java中包私有类的优缺点?