36

I am reading some Java text and the text says that we can only apply public or default access modifier for class and interface. Therefore, it is a compiling error if we declare:

private class A {}

or

protected class A{}

I am just curious why a class or an interface cannot receive private or protected access modifiers?

Autar
  • 1,589
  • 2
  • 25
  • 36
ipkiss
  • 13,311
  • 33
  • 88
  • 123
  • It is not a compiling error if we declare private class A { } or protected class B { } as long as A and B are not top-level classes. – emory Jul 18 '11 at 15:12
  • I believe the default access for interfaces is public, so in effect an interface is always public. – Adriaan Koster Jul 18 '11 at 15:32
  • 1
    @Adriaan Koster No. That rule applies *inside* interfaces, but not to interfaces themselves. – user207421 Jul 19 '11 at 01:19

3 Answers3

54

private means "only visible within the enclosing class".

protected means "only visible within the enclosing class and any subclasses, and also anywhere in the enclosing class's package".

private, therefore, has no meaning when applied to a top-level class; the same goes for the first part of the definition of protected. The second part of protected could apply, but it is covered by the default (package-protected) modifier, so protected is part meaningless and part redundant.

Both private and protected can be (and frequently are) applied to nested classes and interfaces, just never top-level classes and interfaces.

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
16

top level classes can only have public or default access, but internal classes can have private access:

public class TestClassAccess
{
    public static void main(String[] args)
    {
        new TestClassAccess().new TestClassPrivateAccess();
    }

    private class TestClassPrivateAccess
    {
        TestClassPrivateAccess()
        {
            System.out.println("I'm a private class");
        }
    }
}
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • 3
    good examples. internal classes can also be static. internal interfaces and enums are implicitly static. – emory Jul 18 '11 at 15:15
5

There are only two use cases for class visibility at the top level (a) Visible everywhere (b) Visible only within the package. Hence only two modifiers (public and default). If class is public, then it is visible to all classes. If there is no access modifier, then it is visible only for classes within the same package.

Had there been more use cases for class visibility at top level, Java language would have provided more modifiers.

Oak Bytes
  • 4,649
  • 4
  • 36
  • 53
  • Firstly there ARE more cases for visibility than Java provides (see eg c#) and secondly you can have classes with protected or private modifiers - just no top level modifiers. – Voo Jul 18 '11 at 15:13
  • @Voo, there are only two use cases at top level. I believe question implied that as it said any other modifier gave compilation error – Oak Bytes Jul 18 '11 at 15:24
  • visibility of class means? – Half Blood Prince Aug 26 '16 at 06:08
  • if class A can access (create, call member functions etc) class B, then class B is said to be visible to class A. – Oak Bytes Aug 26 '16 at 12:50