private and protected classes
I'm reading the Sierra & Bates book on certification. In multiple spots they state that classes can only be modified by public or default access levels. However I can compile member classes with private and protected access levels. Is it that the compiler is not enforcing the specification?
tx,
No, it's because they couldn't think of a use for a -- or a meaning to assign to a -- top-level protected or private class. You can all access levels with nested classes, however:
public class Outer {
private class Pri {}
class Pac {}
protected class Pro {}
public class Pub {}
}
If your book isn't differentiating between nested and top-level classes, it's not very good.
Nested classes are members, and, as such, all the same access rules make sense for them as for member varialbes and methods.
A private top-level class makes no sense. Only that class could access itself. What would be the point of that?
Likewise, a protected top-level class makes no sense. If a class can only be accessed by classes that inherit from it (and classes in the same package, of course), what's the point of that class? How would you use it?
Public obviously makes sense for top-level classes.
Default (package only) access also makes sense because a package is a group of classes that collaborate closely together, so it's reasonable that those classes might want to share some class among themselves, but not expose it to the world.
jverda at 2007-7-12 20:18:31 >

Good exposition JV, but I must say I've never really got why you can't have a top-level protected class. I can think of lots of uses. All marginally useful of course ... It's a bit like protected & private derivation in C++.
ejpa at 2007-7-12 20:18:31 >

How would a top-level protected class differ from, say, a top-level package-private class?The stuff that "protected" provides for methods and fields in a class, is meaningless for other classes. It's a class that can extend the class that it extends?
http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.htmlIf a class is declared as protected, it can be inherited from its subclass.While a class declared as package-private can't do this.
That's for nested classes. It says that top-level classes can only be public or package-private (default).
If a nested class is protected, then it's available to subclasses of the outer class (not its own subclass).
But suppose that you could apply "protected" to top-level classes. What would it mean?
It would mean that you could extend it from outside the package without having to declare it public.
ejpa at 2007-7-12 20:18:31 >

> Good exposition JV, but I must say I've never really
> got why you can't have a top-level protected class. I
> can think of lots of uses. All marginally useful of
> course ... It's a bit like protected & private
> derivation in C++.
Honestly, I kind of hand-waved on that one. I have a gut feel that top-level protected classes are not particularly (or maybe not at all) useful, but I'm not able to really nail down why. I think my explanation earlier is more or less on track, but definitely not complete. I'm certainly open to somebody telling my why I'm full of shiite.
I do start with the assumption that it was a deliberate design decision that had *some* reasonable (though not necessarily unassailable) logic behind it, even if I'm not able to understand or articulate that logic.
jverda at 2007-7-12 20:18:31 >

It's not something I lose any sleep about, and it's not a feature I've ever actually needed in ten+ years, but when I think about it I'm puzzled why it isn't there. Probably as you say some decision about marginal utility.
ejpa at 2007-7-12 20:18:31 >

I'm going to ignore the fact that classes in the same package can access protected things in the following stream of consciousness because I want to focus on what protected would add relative to package-private.
I wonder what would be the point of making the class protected. If only subclasses could refer to the class, then that means:
* Only subclasses could refer to that class' public methods. In that case, why not just make them protected methods in a public class.
* The parent class can't be used as an abstraction, a general type.
So, what would we gain by allowing protected top-level classes.
jverda at 2007-7-12 20:18:31 >

exactly, it's similar to protected inheritance in C++. Only the subclasses are aware of the inheritance. This is a feature I actually used in C++ at some point in the distant past. Not that that's an argument in its favour ... or in mine ...
ejpa at 2007-7-12 20:18:31 >

You guys are bringing back nightmares of private and protected inheritance in C++.What was that all about -- especially private inheritance?
So, you'd be able to extend the class, but you couldn't delegate to it.
(From the perspective from outside the package.)
If you were a class outside the package, it would be like it didn't exist, except in the cases when you're descended from it.
That feels weird to me. In what applications would this be useful?
When you want to declare a type X that inherits its implementation or some other feature from Y but you don't want the rest of the application to know it's a Y, or to be able to cast to it, etc.
ejpa at 2007-7-21 22:41:14 >

> When you want to declare a type X that inherits its
> implementation or some other feature from Y but you
> don't want the rest of the application to know it's a
> Y, or to be able to cast to it, etc.
But that sounds like this to me, not derivation:
public class X {
private Y y = new Y();
}
Oh well, the less I think about C++ the better.
It is exactly like that except that in C++ you can implement it via private inheritance, without anybody knowing, and without having to write all the delegation code. You can implement 'has-a' by 'be-a' without appearing to be 'is-a'.
ejpa at 2007-7-21 22:41:14 >
