Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Protected use

Started by Lucidquiet Sep 6, 2006 at 1:19 PM 8 replies 1k views
Original Post
Lucidquiet
Lucidquiet
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-
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
Paclaw
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.

Paclaw
superdeveloper
superdeveloper
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.
Paclaw
Paclaw
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 =)
Paclaw
Raghar
Raghar
superdeveloper
Have you noticed that Java has Interfaces build into the language?
You know... 1.A lovely interfaces. 2.If compiler will not optimize them out I'd kill him.
Whackjack
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).
shmoove
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
Michael Nischt
Michael Nischt
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.
Paclaw
Paclaw
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
Paclaw
Whackjack
Whackjack
Good catch. I wasn't aware of the ability to seal a jar file.
But, yeah, for any unsealed jar, you could add it to the same package. We're doing that in the testing packages at my company.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.