SiteMap  | About    

Can a top level class be private or protected

No. A top-level class as private would be completely useless because nothing would have access to it. If a top level class is declared as private the compiler will complain that the "modifier private is not allowed here" . This means that a top level class cannot be private. Private classes are allowed but only as inner or nested classes. If you have a private inner or nested class, then access is restricted to the scope of that outer class.

Can we declare class as protected in java

Protected class member is just like package-private , except that it also can be accessed from subclasses. Defining a field protected makes that field accessible inside the package as well as outside the package through inheritance only (Only inside the subclass). If all the classes are allowed to subclass then it will be similar to public access specifier . Since there is no way to restrict this class being subclassed by only few classes, there is no use of protected access specifiers for top level classes. Hence it is not allowed. (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.