• Post Reply Bookmark Topic Watch Topic
  • New Topic

Protecting a package from being extended  RSS feed

 
Hemant B. Kumar
Greenhorn
Posts: 11
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to protect a Java package from being extended?? What I mean is if I have a package defining a Java class with its access defined as package private and I dont want anyone else to use my package and define new classes for accessing my package private class. Is it possible??
 
Viktor Kubinec
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing such as package access restriction. You can only restrict all classes inside package (just use default access = no modifier for class). Of course there are tools (as Macker) for example) where you can define such rules. If you are using maven or ant for building you can use a macker plugin to simple check if all your rules are satisfied, else the build will fail.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can seal a package inside a JAR file.
 
Stephan van Hulst
Saloon Keeper
Posts: 7984
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can seal your package in a Jar file, and optionally sign it. Take a look at the tutorials: http://docs.oracle.com/javase/tutorial/deployment/jar/sealman.html

[edit]

Rob beat me to it
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!