Protected nested generic constrained inheritance

Discussion in 'Java' started by MRe, Feb 4, 2010.

  1. MRe

    MRe Guest

    Hi,

    Two example programs below, I'm wondering if someone could tell me
    why example 1 doesn't work, given that example 2 does? The error says
    that a nested class has protected access, but I am accessing it from
    an inheriting class

    Thank you,
    Kind regards,
    Eliott

    /////////////////////////////////////////////////////////////////////
    // Example 1
    /////////////////////////////////////////////////////////////////////

    // test/A.java

    package test;

    import test.A.NA;

    public class A<T extends NA>
    {

    protected static class NA
    {
    }

    }

    // test/extend/B.java

    package test.extend;

    import test.A;
    import test.extend.B.NB;

    public class B
    extends A<NB>
    {

    // ERROR : test.A.NA has protected access in nestedtest.A
    protected static class NB
    extends A.NA
    {
    }

    }

    /////////////////////////////////////////////////////////////////////
    // Example 2
    /////////////////////////////////////////////////////////////////////

    // test/A.java

    package test;

    public class A<T>
    {

    protected static class NA
    {
    }

    }

    // test/extend/B.java

    package test.extend;

    import test.A;

    public class B
    extends A<Void>
    {

    // No error
    protected static class NB
    extends A.NA
    {
    }

    }
     
    MRe, Feb 4, 2010
    #1
    1. Advertising

  2. MRe

    Lew Guest

    MRe wrote:
    > Two example programs below, I'm wondering if someone could tell me
    > why example 1 doesn't work, given that example 2 does? The error says
    > that a nested class has protected access, but I am accessing it from
    > an inheriting class
    >
    > /////////////////////////////////////////////////////////////////////
    > // Example 1
    > /////////////////////////////////////////////////////////////////////
    >
    > // test/A.java
    >
    > package test;
    >
    > import test.A.NA;

    ^^
    This import accomplishes nothing.

    > public class A<T extends NA>


    The use of type 'NA' here requires that 'NA' be public. You've exposed 'NA'
    to view beyond same-package or inheriting types.

    > {
    >
    > protected static class NA
    > {
    > }
    >
    > }
    >
    > // test/extend/B.java
    >
    > package test.extend;
    >
    > import test.A;
    > import test.extend.B.NB;

    ^^
    This import accomplishes nothing.

    > public class B
    > extends A<NB>


    'NB' has to extend 'NA', but you are declaring it "above" the inheriting
    scope, so 'NA' is not visible.

    > {
    >
    > // ERROR : test.A.NA has protected access in nestedtest.A


    What *exactly* does the error message state, in its entirety? Copy and paste;
    do not paraphrase or redact.

    > protected static class NB
    > extends A.NA
    > {
    > }
    >
    > }
    >
    > /////////////////////////////////////////////////////////////////////
    > // Example 2
    > /////////////////////////////////////////////////////////////////////
    >
    > // test/A.java
    >
    > package test;
    >
    > public class A<T>


    This declaration does not elevate any protected member to public view.

    > {
    >
    > protected static class NA
    > {
    > }
    >
    > }
    >
    > // test/extend/B.java
    >
    > package test.extend;
    >
    > import test.A;
    >
    > public class B
    > extends A<Void>


    This declaration does not require a public view of any protected member.

    > {
    >
    > // No error
    > protected static class NB
    > extends A.NA


    This declaration is contained within the declaration scope of an inheriting
    class and is therefore legal.

    > {
    > }
    >
    > }


    --
    Lew
     
    Lew, Feb 4, 2010
    #2
    1. Advertising

  3. MRe

    markspace Guest

    MRe wrote:
    > The error says
    > that a nested class has protected access, but I am accessing it from

    ^^^^^^^^^
    > an inheriting class

    ^^^^^^^^^^


    Now go read up on what protected actually does (hint: I used Google to
    find this, it was the first link):

    <http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html>
     
    markspace, Feb 4, 2010
    #3
  4. MRe

    MRe Guest

    Hi Lew,

    Thank you for the response..

    > > import test.A.NA;

    >
    >                  ^^
    > This import accomplishes nothing.


    That's what I thought, but NetBeans6.8 seems to insist

    > > public class A<T extends NA>

    >
    > The use of type 'NA' here requires that 'NA' be public.  You've exposed 'NA'
    > to view beyond same-package or inheriting types.


    Oh, well that makes sense. I guess I missed it because Java didn't
    complain. I see now that it will compile in the absence of B so long
    as I keep every thing in the test package. I was mixing up several
    different things; sorry. Thank you :)

    > >    // ERROR : test.A.NA has protected access in nestedtest.A

    >
    > What *exactly* does the error message state, in its entirety?  Copy and paste;
    > do not paraphrase or redact.


    That was the whole error.

    > > public class A<T>

    >
    > This declaration does not elevate any protected member to public view.
    >
    > > public class B
    > > extends      A<Void>

    >
    > This declaration does not require a public view of any protected member.
    >
    > >    // No error
    > >    protected static class NB
    > >    extends                A.NA

    >
    > This declaration is contained within the declaration scope of an inheriting
    > class and is therefore legal.


    Understood. Thank you

    > Lew


    Thanks again,
    Kind regards,
    Eliott
     
    MRe, Feb 5, 2010
    #4
  5. MRe

    MRe Guest

    On Feb 4, 10:13 pm, markspace <> wrote:
    > MRe wrote:
    > > The error says
    > > that a nested class has protected access, but I am accessing it from

    >                           ^^^^^^^^^
    > an inheriting class
    >    ^^^^^^^^^^
    >
    > Now go read up on what protected actually does (hint: I used Google to
    > find this, it was the first link):
    >
    > <http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html>


    Hi markspace,

    Thank you for the response and interesting link.

    I'm not sure I understand the mistake I've made in the quote you've
    highlighted (given the context in which you've quoted it) - as far as
    I can tell, the linked document affirms what I've said?

    Thank you again for the link; a concise and very useful find.
    Kind regards,
    Eliott
     
    MRe, Feb 5, 2010
    #5
  6. MRe

    MRe Guest

    Hi again markspace,

    >   I'm not sure I understand the mistake I've made in the quote you've
    > highlighted (given the context in which you've quoted it) - as far as
    > I can tell, the linked document affirms what I've said?


    I just stepped away there and had a think about what you said, and
    sorry, I understand what you mean now.

    Thank you
    Kind regards,
    Eliott
     
    MRe, Feb 5, 2010
    #6
  7. The title of this post made me wonder if Java has gotten too complicated for
    its own good. It sounds like one of the obscurer features of C++.
     
    Mike Schilling, Feb 5, 2010
    #7
  8. MRe

    Roedy Green Guest

    On Thu, 4 Feb 2010 10:10:09 -0800 (PST), MRe <> wrote,
    quoted or indirectly quoted someone who said :

    >
    > Two example programs below, I'm wondering if someone could tell me
    >why example 1 doesn't work, given that example 2 does? The error says
    >that a nested class has protected access, but I am accessing it from
    >an inheriting class


    Protected is not public. Protected methods are invisible outside the
    class, unless that class extends the base class.

    See http://mindprod.com/jgloss/protected.html
    http://mindprod.com/jgloss/scope.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    You can’t have great software without a great team, and most software teams behave like dysfunctional families.
    ~ Jim McCarthy
     
    Roedy Green, Feb 5, 2010
    #8
  9. MRe

    Roedy Green Guest

    On Thu, 04 Feb 2010 20:50:15 -0800, Roedy Green
    <> wrote, quoted or indirectly quoted
    someone who said :

    >Protected is not public. Protected methods are invisible outside the
    >class, unless that class extends the base class.


    oops.

    Protected is not public. Protected methods are invisible outside the
    PACKAGE, unless the referring class extends the base class.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    You can’t have great software without a great team, and most software teams behave like dysfunctional families.
    ~ Jim McCarthy
     
    Roedy Green, Feb 5, 2010
    #9
  10. MRe

    Roedy Green Guest

    On Thu, 4 Feb 2010 10:10:09 -0800 (PST), MRe <> wrote,
    quoted or indirectly quoted someone who said :

    >
    > Two example programs below, I'm wondering if someone could tell me
    >why example 1 doesn't work, given that example 2 does? The error says
    >that a nested class has protected access, but I am accessing it from
    >an inheriting class


    see http://java.sys-con.com/node/35776
    on extending inner classes. Unfortunately the associated listings
    seem to have gone astray. I have written Sys-Con.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    You can’t have great software without a great team, and most software teams behave like dysfunctional families.
    ~ Jim McCarthy
     
    Roedy Green, Feb 5, 2010
    #10
  11. MRe

    Lew Guest

    MRe wrote:
    >>> // ERROR : test.A.NA has protected access in nestedtest.A


    Lew wrote:
    >> What *exactly* does the error message state, in its entirety? Copy and paste;
    >> do not paraphrase or redact.


    MRe wrote:
    > That was the whole error.


    What? No line number? No reprint of the offending line? It started with
    double slashes?

    I don't believe you.

    --
    Lew
     
    Lew, Feb 5, 2010
    #11
  12. MRe

    Arne Vajhøj Guest

    On 04-02-2010 21:41, Mike Schilling wrote:
    > The title of this post made me wonder if Java has gotten too complicated for
    > its own good. It sounds like one of the obscurer features of C++.


    It has not quite reached C++ level, but if they keep adding
    features because C# has it, Ruby has it etc., then it will.

    And most likely it will end up like previous "do everything
    in universe" languages like PL/I and Ada.

    Arne
     
    Arne Vajhøj, Feb 6, 2010
    #12
    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. cltsaig

    Port "arg" is not constrained?

    cltsaig, Oct 12, 2004, in forum: VHDL
    Replies:
    2
    Views:
    730
    cltsaig
    Oct 12, 2004
  2. Replies:
    9
    Views:
    8,027
    Ralf Hildebrandt
    Jan 19, 2005
  3. Andreas Klemt
    Replies:
    2
    Views:
    569
    Andreas Klemt
    Jul 5, 2003
  4. Slawek Grabowski

    Constrained-random verification.

    Slawek Grabowski, Oct 25, 2006, in forum: VHDL
    Replies:
    2
    Views:
    868
    Slawek Grabowski
    Oct 26, 2006
  5. Replies:
    0
    Views:
    344
Loading...

Share This Page