access modifier in Java

There are four access controls in Java but only three access modifiers. The fourth access control level (called default or package access) is what you get when you don't use any of the three access modifiers. Every class, method, and instance variable has an access control, whether you explicitly type one or not. An access modifier can not be applied to a local variable.
name |keyword | maximal access |
---- | --- | --- |
private | private | within the same class |
package| - | within its package |
protected | protected | derived classes via inheritance or within same package |
public | public | public |

There are two different access issues:
* Whether method code in one class can access a member of another class
* Whether a subclass can inherit a member of its superclass

<b>Class:</b>
A class can be declared with only public or default access; the other two access control levels don't make sense for a class. If you want the class used only in its package, use default access; otherwise make it public.
A top level class cannot be protected or private. 
* Protected class member (method or variable) is just like package-private (default visibility), except that it also can be accessed from subclasses. Since there's no such concept as 'subpackage' or 'package-inheritance' in Java, declaring class protected or package-private would be the same thing. You can declare nested and inner classes as protected, though.
* 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. If you have a private class on its own as a top-level class, then you can't get access to it from anywhere. So it does not make sense to have a top level private class.

<b>Class Members:</b>
| Visibility | Public | Protected | Default | Private |
| --- | --- | --- | --- | --- |
| From the same class | Yes| Yes| Yes| Yes|
| From any class in the same package | Yes| Yes| Yes| No|
| From a sub-class in the same package | Yes| Yes| Yes| No|
| From a sub-class outside the same package | Yes| Yes, through inheritance | No| No|
| From any non-sub-class outside the same package | Yes| No | No| No|