2

What could have been the design decision behind restricting a class to either public or default level access? The protected member is more visible than a default, then why skip protected?

EDIT: I meant TOP level classes

Varun Achar
  • 14,781
  • 7
  • 57
  • 74

4 Answers4

4

The visibility level of classes is not the same as that of members!

With members the whole idea of subclassing comes into play, hence the notion of "protected."

But with classes, you are either visible outside your package or you are not. There is no real notion of being or not being visible to your subclasses. It would be odd, wrong actually, if a subclass could not see its superclass.

In addition, subpackages don't really work like subclasses, so again, "protected" on classes just really doesn't work.

BTW we are talking here of top-level classes, right? Member classes are a different story.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
0

We can only guess about specific design decision, but protected is necessary so external to your code components could extend your components. And default visibility just offers sharing within your own package without exposing it to external code.

Note: My answer applies to classes only.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
0

Quoto from this:

Modifier    | Class | Package | Subclass | World

public      |  Y    |    Y    |    Y     |   Y

protected   |  Y    |    Y    |    Y     |   N

no modifier |  Y    |    Y    |    N     |   N

private     |  Y    |    N    |    N     |   N

The major different between default and protected is that protected can be access by subclass, but default cannot. And the design decision between public and default is that one can access from other package but the other cannot.

Both comparasions are compared two different things.

Community
  • 1
  • 1
TheOneTeam
  • 25,806
  • 45
  • 116
  • 158
-1

There has been no such design decision. Classes can be public, protected, package private (or default), and or private.

class TopLevelClass
{
     public class PublicClass
     {
     }

     protected class ProtectedClass
     {
     }

     /*package private*/ class PackagePrivateClass
     {
     }

     private class PrivateClass
     {
     }
}
emory
  • 10,725
  • 2
  • 30
  • 58
  • I think the OP is talking about top-level classes. You are right about member classes, though. – Ray Toal Jul 24 '11 at 05:41
  • ever tried to compile a private class? This is what the compiler spits out "Illegal modifier for the class XYZ; only public, abstract & final are permitted" – Varun Achar Jul 24 '11 at 05:42