private, nested public, class visible outside package

Discussion in 'Java' started by MRe, Jul 4, 2008.

  1. MRe

    MRe Guest

    Hi,

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

    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)

    Thank you,
    Kind regards,
    Eliott

    --More detail as promised--

    If I have a package-private class called 'DataSet', and nested in
    DataSet, a public class 'Stats', and in another file, a package-public
    class 'Graph', that could return objects of type DataSet.Stats, I
    would want, outside the package to say..

    DataSet.Stats stats = graph.getStats();

    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?

    I know I could fix it by factoring the nested Stats part out of
    DataSet, but it just feels right nested in DataSet

    --Pseudo Java outline of the code--

    // A class for displaying a graph
    // and some stats in an app
    package App
    public class GraphPane
    { Graphables.Graph graph;
    public void show()
    { Graphables.DataSet.Stats stats;
    stats = graph.getStats();
    }
    }
    // A package that embodies graphness
    package Graphables;
    // A graph with a set of data
    public class Graph
    { DataSet dataSet;
    public Graphables.DataSet.Stats getStats()
    { return dataSet.getStats();
    }
    }
    // A set of data; in a package-private class
    // Don't want the data outside, as only the
    // Graph is to know about it
    class DataSet
    { Stats stats;
    float[] data;
    public Graphables.DataSet.Stats getStats()
    { return stats;
    }
    // Stats (which, in my graphable world) are
    // tightly linked with DataSet, and exist
    // within data, but is also something that I
    // need visible outside the package, because
    // the graph doesn't show them
    public class Stats
    { float calcMean()
    { float mean = 0;
    for(float datum : data)
    mean += datum;
    return mean /= data.length;
    }
    }
    }

    Thanks again; for reading this far :)
     
    MRe, Jul 4, 2008
    #1
    1. Advertising

  2. Peter Duniho wrote:
    > On Fri, 04 Jul 2008 09:27:15 -0700, MRe <> 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.


    Even if it were possible, it would not be desirable. It would make the
    existence of DataSet, and some of its implementation, visible outside
    its package.

    > 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.


    Strongly agree. Indeed, I typically use an interface for a public method
    result, regardless of how the underlying class is implemented. That
    leaves me free to nest inside non-public classes, replace with two or
    more different classes for different implementation situations etc.
    without affecting callers.

    Patricia
     
    Patricia Shanahan, Jul 4, 2008
    #2
    1. Advertising

  3. MRe

    MRe Guest

    On Jul 4, 5:40 pm, "Peter Duniho" <>
    wrote:
    > On Fri, 04 Jul 2008 09:27:15 -0700, MRe <> 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 :( ..

    > 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)

    > Pete


    Kind regards,
    Eliott
     
    MRe, Jul 4, 2008
    #3
  4. MRe

    MRe Guest

    On Jul 4, 6:12 pm, Patricia Shanahan <> wrote:
    > Peter Duniho wrote:
    > > On Fri, 04 Jul 2008 09:27:15 -0700, MRe <> 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.

    >
    > Even if it were possible, it would not be desirable. It would make the
    > existence of DataSet, and some of its implementation, visible outside
    > its package.


    Digable; I kinda figured if Java was complaining about it, there was
    something off, but wasn't sure what as I liked the look of it. So long
    as there was roughly a similar solution.

    > > 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.

    >
    > Strongly agree. Indeed, I typically use an interface for a public method
    > result, regardless of how the underlying class is implemented. That
    > leaves me free to nest inside non-public classes, replace with two or
    > more different classes for different implementation situations etc.
    > without affecting callers.


    This works good for me. I'm trying my best to be as OO and extensible
    as possible, though I seem to spend more time refactoring the code
    than programming, but if it gets me a neat solution, I'm happy

    > Patricia


    Thank you mucho for the response,
    Kind regards,
    Eliott
     
    MRe, Jul 4, 2008
    #4
  5. MRe

    Roedy Green Guest

    On Fri, 4 Jul 2008 09:27:15 -0700 (PDT), MRe <> 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
     
    Roedy Green, Jul 4, 2008
    #5
  6. Peter Duniho wrote:
    > On Fri, 04 Jul 2008 09:27:15 -0700, MRe <> 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.
     
    Mike Schilling, Jul 4, 2008
    #6
  7. MRe

    Tom Anderson Guest

    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.
     
    Tom Anderson, Jul 5, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    1
    Views:
    1,445
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    641
    SpamProof
    Oct 21, 2003
  3. qazmlp
    Replies:
    19
    Views:
    828
    Daniel T.
    Feb 4, 2004
  4. DaveLessnau
    Replies:
    3
    Views:
    449
    Howard
    May 16, 2005
  5. xeon Mailinglist
    Replies:
    3
    Views:
    176
    Andreas Leitgeb
    Feb 14, 2014
Loading...

Share This Page