Understanding class access

  • Thread starter =?iso-8859-1?q?Andr=E9_Wagner?=
  • Start date
?

=?iso-8859-1?q?Andr=E9_Wagner?=

Hello,

let's say I'm writing a extension organized in packages such as:

mycompany.mypackage.MyClass
mycompany.mypackage.subpackage.MySubClass

The people who are going to use my extension will extend MyClass. So I
declare it to be public.

But I don't want people to be able to access MySubClass, so I can't
declare as public. But I want MyClass to be able to access MySubClass,
and if I don't declare MySubClass to be public, the compiler complains
that MySubClass is in a different package and must be public.

(I hope I'm being clear here :)

Is there anyway that I can access MySubClass from MyClass, but make
MySubClass invisible for packages outside mycompany.mypackage?

Thank you in advance,

André
 
C

Chris Smith

André Wagner said:
let's say I'm writing a extension organized in packages such as:

mycompany.mypackage.MyClass
mycompany.mypackage.subpackage.MySubClass

Is there anyway that I can access MySubClass from MyClass, but make
MySubClass invisible for packages outside mycompany.mypackage?

No, because in fact, though the names look herarchical, your
mycompany.mypackage.subpackage is outside of mycompany.mypackage. (As a
side note, if MySubClass is a subclass of MyClass, then trying to access
MySubClass from MyClass is a cyclic dependency anyway.)

It's hard to suggest more than that without knowing why you don't want
anyone outside of mycompany.mypackage to access MySubClass. Is it
simply an organizational principle? If so, then you'll notice a lot of
packages in the standard librari have "implementation" or "provider"
subpackages, and just rely on documentation to say that others shouldn't
use it. If it's a security issue, such as when running untrusted code
in the VM, then you'll have to redesign a bit more than that.
 
A

angrybaldguy

Hello,

let's say I'm writing a extension organized in packages such as:

mycompany.mypackage.MyClass
mycompany.mypackage.subpackage.MySubClass

The people who are going to use my extension will extend MyClass. So I
declare it to be public.

But I don't want people to be able to access MySubClass, so I can't
declare as public. But I want MyClass to be able to access MySubClass,
and if I don't declare MySubClass to be public, the compiler complains
that MySubClass is in a different package and must be public.

(I hope I'm being clear here :)

Is there anyway that I can access MySubClass from MyClass, but make
MySubClass invisible for packages outside mycompany.mypackage?

Thank you in advance,

André

If you don't want MySubClass to be subclassed, declare it final --
that's what it's there for.
 
?

=?iso-8859-1?q?Andr=E9_Wagner?=

If you don't want MySubClass to be subclassed, declare it final --
that's what it's there for.

What I mean is: I don't want it to be accessed.

André
 
T

Twisted

What I mean is: I don't want it to be accessed.

If you don't want it confusing people or "cluttering things up", just
don't include the javadoc for mysubpackage in your released
documentation bundle, or include in the package-info.html that it's a
private subpackage of little interest to most users.

If you really want to completely deny access to MySubclass except via
MyClass, make it a private nested class of MyClass.
 
A

angrybaldguy

Hello,

let's say I'm writing a extension organized in packages such as:

mycompany.mypackage.MyClass
mycompany.mypackage.subpackage.MySubClass

The people who are going to use my extension will extend MyClass. So I
declare it to be public.

But I don't want people to be able to access MySubClass, so I can't
declare as public. But I want MyClass to be able to access MySubClass,
and if I don't declare MySubClass to be public, the compiler complains
that MySubClass is in a different package and must be public.

(I hope I'm being clear here :)

Is there anyway that I can access MySubClass from MyClass, but make
MySubClass invisible for packages outside mycompany.mypackage?

Thank you in advance,

André

If you don't want MySubClass to be subclassed, declare it final --
that's what it's there for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top