Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of package level protection in java?

Tags:

I know how package level protection in java works. I read a lot of code (including lots of open source stuff) and no-one seem to be using it. The whole protection level seems slightly faulty to me (I'd have c#'s internal any day of the week).

Are there any legit real-world use-cases that are in common use ?

Edit: A little too late after asking this question I realized I'd forgotten to exclude the "standard" pattern of package protected implementation classes, possibly providing implementations of public interfaces. Everyone uses those, as has been noted several times in the replies. I still think there are a large number of nice replies to this question.

like image 691
krosenvold Avatar asked Dec 31 '08 17:12

krosenvold


People also ask

What is package protected in Java?

Protected member can be accessed from non-child classes of the same package. Package member can be accessed from non-child class of the same package. Public members can be accessed from the child class of outside package. Private members cannot be accessed from the child class of outside package.

What is the use of a protected class in a package?

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

What is package level in Java?

package level access is the default access level provided by Java if no access modifier is specified. These access modifiers are used to restrict the accessibility of a class, method, or variable on which it applies.

Why we use protected in Java?

Protecting a constructor prevents the users from creating the instance of the class, outside the package. During overriding, when a variable or method is protected, it can be overridden to other subclass using either a public or protected modifier only. Outer class and interface cannot be protected.


1 Answers

There are two good uses for package level visibility (in my experience):

1) Defining "internal" classes in a public API. Commonly you would define your interfaces and core factories as public and the "internal" implementations as package level. Then the public factories can construct the package level implementation classes and return them as instances of the public interfaces. This nicely allows users to only access the stuff they should.

The downside is that you have to have all this stuff in the same package, which almost never is a good idea for any reasonably-sized API. JSR 294/modules/Project Jigsaw in Java 7 will hopefully provide an alternative by specifying a new visibility modifier (module) that can be used to access classes within a module across packages without making them visible outside the module. You can find an example of how this would work in this article.

2) Unit testing is the other common use case. Frequently you'll see a src tree and a test tree and stuff that would otherwise be private is instead package level so that unit tests in the same (parallel) package are able to access otherwise hidden methods to check or manipulate state.

like image 79
Alex Miller Avatar answered Oct 12 '22 17:10

Alex Miller