Sign in to follow this  
Lucidquiet

[java] Protected use

Recommended Posts

Is there a way to limit access ONLY to those classes inheriting from a class? Private won't work because it's technically no inherited, and protected isn't quite the same because it allows package access. Public is way to much access. Any ideas? L-

Share this post


Link to post
Share on other sites
No.

Protected is the way to go to have visibilty in inheritance. But does it really matters that Classes in the packages sees it? Classes in a package are somewhat "friends" and you have complete control on them.

Share this post


Link to post
Share on other sites
Perhaps you can make use of interfaces (see DECLARE_INTERFACE and __interface) to restrict what is visible, and then create sub-interfaces (same notion as subclasses) for the inherited classes?

Actually I'm not sure if that is what you want but it may be a direction to investigate, but I'm not 100% sure I understood your original question.

Share this post


Link to post
Share on other sites
Quote:
Original post by superdeveloper
Actually I'm not sure if that is what you want but it may be a direction to investigate, but I'm not 100% sure I understood your original question.

It's because we talk about java in the java forum =)

Share this post


Link to post
Share on other sites
Quote:
Original post by Paclaw
No.

Protected is the way to go to have visibilty in inheritance. But does it really matters that Classes in the packages sees it? Classes in a package are somewhat "friends" and you have complete control on them.


Not always. I could easily declare a class to be in the java.lang.* package and would get access to all the protected and package-private methods and variables.
Unfortunately, there isn't really a way around this (at least not in my experience).

Share this post


Link to post
Share on other sites
Quote:
Original post by Whackjack
I could easily declare a class to be in the java.lang.* package and would get access to all the protected and package-private methods and variables.

You can declare a class to be in the java.lang.* package? Have you tried? Because AFAIK you're not allowed to add classes to the java.* or javax.* hierarchies. At least that's the case in J2ME.

shmoove

Share this post


Link to post
Share on other sites
Quote:
Original post by Whackjack
Quote:
Original post by Paclaw
No.

Protected is the way to go to have visibilty in inheritance. But does it really matters that Classes in the packages sees it? Classes in a package are somewhat "friends" and you have complete control on them.


Not always. I could easily declare a class to be in the java.lang.* package and would get access to all the protected and package-private methods and variables.
Unfortunately, there isn't really a way around this (at least not in my experience).


There is more than one way to get around this, see this article for details: Controlling Package Access With Security Permissions and Sealed JAR Files

The easiest way is to seal your jar file by adding the follwing line to the manifest: Sealed: true
The result is:
Quote:
After a class loader loads a class from a sealed JAR file, classes in the same package can only be loaded from that JAR file

Of course there are alot of more fine grained solutions, but often this is often sufficient.

Share this post


Link to post
Share on other sites
Quote:
Original post by shmoove
Quote:
Original post by Whackjack
I could easily declare a class to be in the java.lang.* package and would get access to all the protected and package-private methods and variables.

You can declare a class to be in the java.lang.* package? Have you tried? Because AFAIK you're not allowed to add classes to the java.* or javax.* hierarchies. At least that's the case in J2ME.

shmoove


you are right!

java.lang.SecurityException: Prohibited package name: java.lang
but for a user package Whackjack's comment would work.

I think the best way is to seal. But this kind of suck if you are in development and you want to restrict access. Well nothing is perfect... Even Java

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this