2
import java.util.*;

public class NewTreeSet2{
    void count(){
        for (int x=0; x<7; x++,x++){
            System.out.print(" " + x);
        }
    }
}
protected class NewTreeSet extends NewTreeSet2{
    public static void main(String [] args){
        NewTreeSet2 t = new NewTreeSet2();
        t.count();
    }
}

Here, I cannot make the NewTreeSet sub class as protected. Why is this? I am not trying to accomplish anything, this is only for my understanding of the access specifiers.

3 Answers3

3

public is the only access-modifier that can explicitly be applied to a top level class in Java. The protected modifier in case of a class can only be applied to inner classes.

Section 8.1.1 of the Java language specification says this :

The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration

So why can't top level classes be marked as protected? The purpose of protected access modifier in Java is to add restrictions on the access to a member of a class. Since a top level class is not a member of any class, the protected access modifier does not make sense for top level classes. Inner classes can be marked as protected because they are indeed members of a class. The same rules apply for private as well.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
0

A class definition itself can only be public or package-private. Public classes must be defined in their own file and the filename must be the class name.

The documentation doesn't say anything about why there are only two access modifiers for the top level, but one might say that it is logical:

  • The protected access modifier makes sure only instances of the same or a child class can access the subject. Polymorphism is not applicable to classes, only to instances of classes. Thus the protected keyword there doesn't make sense.

If you want to protect the construction of objects, you should specify an access modifier to your constructor.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
0

"Why can't we have the 'protected' modifier for a top-level class".

Assume it's allowed to use protected modifier for a class. Then what will happen, it will be visible to all the classes in the same package which is the same behavior what a default (package-level) access class will possess. Additionally this 'protected' class should be visible to all the subclasses outside package also. But unfortunately you would not be able to create any subclass of this class outside the package because this class itself will not be visible outside the package. Hence without the subclass specific behavior, this 'protected' class will be exactly same as a package-level or default access class. So, there is absolutely no need of 'protected' modifier for classes and hence, not permissible as well.

---This was posted in a different forum, by B Verma, found this answer according to which all of you said. It was really helpful, thank you.

http://www.coderanch.com/t/585021/java/java/Protected-access-modifier-class-level