From: Lew on
MRe wrote:
> On Jul 4, 5:40 pm, "Peter Duniho" <NpOeStPe...(a)nnowslpianmk.com>
> wrote:
>> On Fri, 04 Jul 2008 09:27:15 -0700, MRe <pgd...(a)gmail.com> wrote:
>>> [...]
>>> I have a class that is private within a package, but contains a
>>> nested class that I want to be public outside the package, i.e.
>>> Outside the package, the non-nested class cannot be touched, but the
>>> nested class can. This nested class is accessed by asking another
>>> public class in the package to return it
>>> Is this possible? (More detail below, if that doesn't make sense)
>> This isn't possible, not literally.
>
> Awh, it isn't, that's a shame :( ..

No, it's not. It's a Good Thing.

>> However, you can achieve the same effect by defining a public interface
>> that includes all the public members of the nested class that you want to
>> have, and then declaring the nested class as implementing that interface.
>> Then the instance of the nested class can be exposed as an implementer of
>> the interface, without requiring the code using it to be able to see the
>> nested class itself.
>
> ..when suddenly, Bam! Solution! :)
>
> This works great, I like it; fits in really nice.
> Thank you very much, (plus for the quick response)

Especially when the language supports in a proper fashion what you want to do.

Doing what you first requested would break access safety. Access safety is
the whole *point* of private declarations. Making such an easy way to break
it would not be good. Preventing it is not a shame.

--
Lew
From: Roedy Green on
On Fri, 4 Jul 2008 09:27:15 -0700 (PDT), MRe <pgdown(a)gmail.com> wrote,
quoted or indirectly quoted someone who said :

> But this doesn't compile, instead saying that DataSet cannot be
>found; which seems simple enough, I can't always do whatever I want!
>But is it that simple. Or is there some little [or big] thing I can do
>to do what I want to do?

One other possibility, is to have the inner class extend a public
abstract class. You then return that type. This is a variation on
returning an interface.

See http://mindprod.com/jgloss/interfacevsabstract.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
From: Mike Schilling on
Peter Duniho wrote:
> On Fri, 04 Jul 2008 09:27:15 -0700, MRe <pgdown(a)gmail.com> wrote:
>
>> [...]
>> I have a class that is private within a package, but contains a
>> nested class that I want to be public outside the package, i.e.
>> Outside the package, the non-nested class cannot be touched, but
>> the
>> nested class can. This nested class is accessed by asking another
>> public class in the package to return it
>>
>> Is this possible? (More detail below, if that doesn't make sense)
>
> This isn't possible, not literally.
>
> However, you can achieve the same effect by defining a public
> interface that includes all the public members of the nested class
> that you want to have, and then declaring the nested class as
> implementing that interface. Then the instance of the nested class
> can be exposed as an implementer of the interface, without requiring
> the code using it to be able to see the nested class itself.


Note that this is a common pattern in Java. Arrays.asList(Object[])
creates a nested class whose implementation details (and even name)
are no one's business, but it's perfectly usable because it implements
List. And this List class has a further nested class to iterate it,
which again is no one's business and again is perfectly usable, this
time because it implements Iterator.


From: Tom Anderson on
On Fri, 4 Jul 2008, MRe wrote:

> This is probably more a design than programming question, but I can't
> find a Java group for that[?]

cljp is fine - design, of this kind, is a part of programming. However,
the other place you could consider would be comp.object.

tom

--
Don't anthropomorphize computers: they don't like that.