Package Visibility

Why use package visibility (default) if the class should not be public in java

+6
java visibility abstraction package
source share
3 answers

As Rostislav Matl said, this is useful when you want to do something that is not part of your package interface.

As an example, suppose you have a package and it provides an interface and at least one specific implementation of a service.

People who use this service will take care of the interface you provide and use one of the specific classes that you provide, but they will not care about much other than that. Our service should talk to the database, and it should be able to display the result from database queries in its own data type (which form its contract).

I find that I regularly create classes in the package’s closed helper that contain methods like utilities or perform tasks such as the mapping we need. The default visibility (batch) is great for this because the other classes inside your package can use these helpers, but none of them can see them, so you can change them whenever you want.

This is an example of using some code:

We have our interface:

public interface UsefulService { Collection<DataThings> getThings(Identifier id); } 

... and our specific implementation:

 public class JdbcUsefulServiceImpl implements UsefulService { //We can break the code for the mapping out into its own class private Mapper mapper; @Override public Collection<DataThings> getThings(Identifier id){ DatabaseQueryResult queryResult = //Code to hit a database and return objects from that domain model Collection<DataThings> result = mapper.mapFromDatabaseToServiceDomain(queryResult); return result; } } 

Then we have our cartographer. We don’t need anyone outside the package to take care of the work of the service inside, so we use the individual visibility of the package and we can have as many classes as we want to execute:

 class Mapper { Collection<DataThings> mapFromDatabaseToServiceDomain(DatabaseQueryResult queryResult){ //magic to map objects goes here } } 

The advantage that we have is that we can always change this Mapper class, but we either want to delete it, or create new private package classes, and we know that the only (immediate) effects that we can cause are inside this package. By direct effects, I mean compiler errors and such serious things. Obviously, you can break your service if you change its behavior, but what your automatic test suite should catch: P

+7
source share

My misunderstanding is package / default access for internal package components, i.e. classes that do not form the package interface , i.e. classes that should not be used outside the package.

+3
source share

What you get from the default ie modifier, without the access modifier (i.e., open or closed), this means that it is displayed to everyone inside a specific package. Therefore, when you want your class to be available only in its own package, you should go to default .

Read more: Access Control

0
source share

All Articles