Protected nested generic constrained inheritance

M

MRe

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
{
}

}
 
L

Lew

MRe 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

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

MRe

Hi Lew,

Thank you for the response..
                 ^^
This import accomplishes nothing.

That's what I thought, but NetBeans6.8 seems to insist
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 :)
What *exactly* does the error message state, in its entirety?  Copy and paste;
do not paraphrase or redact.

That was the whole error.
This declaration does not elevate any protected member to public view.


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


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

Understood. Thank you

Thanks again,
Kind regards,
Eliott
 
M

MRe

                           ^^^^^^^^^
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
 
M

MRe

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
 
M

Mike Schilling

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

Roedy Green

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
 
R

Roedy Green

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
 
R

Roedy Green

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
 
A

Arne Vajhøj

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
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top