0

I have created a JAR project which consists of two packages :

  • package a : contains some methods
  • package b : contains methods calling the ones of package a

Now I want that the projects using my JAR should not be able to call methods in package a, instead they must call methods defined in package b.

kryger
  • 12,906
  • 8
  • 44
  • 65
Mohit Garg
  • 55
  • 1
  • 5
  • 2
    if a jar is on the classpath then (generally speaking) everything else on the classpath has access to its public classes and their public methods. – Software Engineer Oct 28 '15 at 16:48
  • 1
    You can have a look to http://stackoverflow.com/questions/5872124/how-to-hide-the-internal-structure-of-a-java-api-to-the-rest-of-the-world. What is your use case exactly ? – Gaël J Oct 28 '15 at 17:09
  • @Gaël: I created a jar with DAO layer and Service Layer. This jar is being used in other projects by different teams. I want to restrict other calling projects from directly accessing DAO layer, instead they should use the services exposed in Service Layer interface only. – Mohit Garg Oct 28 '15 at 17:58
  • @MohitGarg : at some point you have to assume people are using your API correctly. Just tell them they should only use Service Layer. – Gaël J Oct 28 '15 at 18:50

2 Answers2

0

Use default access modifier

If you don't use any modifier for a variable or method , it is treated as default access modifier. The default modifier is accessible only within package.

M Sach
  • 33,416
  • 76
  • 221
  • 314
0

I think what you want to do is not posible in java with the current modifiers.

However I recommend you to do:

  1. Define the methods in package[A] with protected modifier.
  2. In package[B] create a proxy class. This class must inherit from its counterpart in package[A]. This class will encpsulate the methods of its similar class in Packacge[A] and will make the methods calls to the package[A] counterpart.
  3. In package[B] you have to make the calls through proxy class.
reos
  • 8,766
  • 6
  • 28
  • 34