Protected Access Applied to a Class

The protected access modifier cannot be applied to a standard outer class. In Java, sometimes it matters just as much what you cannot do versus what you can do. You do not want to by that guy that raises his hand in a meeting and suggests that we make the class protected. Okay, what happens when we try to apply the protected access to a class? Let's do it.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md one
C:\Java>cd one
C:\Java\one>Notepad Tester.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


package one; // good practice

protected class Tester { }

Now switch back to the command prompt (CMD) and type in the following commands.


C:\Java\one>javac Tester.java
Tester.java:3: error: modifier protected not allowed here
protected class Tester { }
          ^


Final thoughts

The only modifier that can be applied to a standard outer class is public. A standard outer class without a modifier is implicitly assigned default (package-private) access.


Tutorials