Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make java class visible only within the project/app workspace

Tags:

java

i am developing a SDK in java and my issue is hiding certain java classes/packages so that no third party application using this sdk is exposed to these underlining class's

I know that if all class objects in the sdk were all inside one package i can assign default/protected access modifiers to the class i want to hide. However, like lots of java projects, a java project like my sdk consists of different packages that group the relivant java class's appropriately.

This means that if i want to hide/protect a class thats in com.a.b.c, The main root class thats visible to the world located in com.a wont be able to access classes protected in com.a.b.c etc

How can i provide a solution to this instead of just dumping all classes in one large package?

Thanks.

like image 865
Jonathan Avatar asked Oct 20 '22 11:10

Jonathan


2 Answers

The Java namespace and access modifiers are not hierarchical, even if that seems to be as of the namespace structure made of folders. Java really has a flat namespace with arbitrary full qualified names (dot notation).

So if you want to close-up your library classes and use it in the library the same time, there is no other way to put them in the same package. Maybe best to explain: when you use a class outside the package this class makes part of the API, as every package in Java is a module.

like image 100
PeterMmm Avatar answered Oct 23 '22 09:10

PeterMmm


You could factory classes in the sub-packages and use interfaces. That way you can hide your implementation classes.

like image 20
mikea Avatar answered Oct 23 '22 11:10

mikea