Java Access Modifiers

One of the main principle of Object Oriented Programming is 'information hiding', which means that objects don't reveal all of their details to the outside world. Just like other object-oriented programming languages, Java has access modifiers to restrict access to members of classes, traits, objects and packages. Access Modifiers control how much of an object is visible to the rest of Java program, and packages provide the highest level of control over object visibility. For ex: a class that is not public is not visible outside its own package. There are 4 different access modifiers available in java language. They are public, protected, private and no modifier (declaring without an access modifier). Using ‘no modifier’ is also sometimes referred as ‘package-private’ or ‘default’ or ‘friendly’ access.

Java Access Modifiers


If a method or variable is marked as private or has the private access modifier assigned to it; it cannot be accessed by any object of any other class, although they can be accessed from other methods defined by the same class. That is, neither the code inside subclasses cannot access the variable or method, nor can code from any external class. This is useful where you want to prevent subclasses from modifying variables except through the controls provided and deny that ability to everyone else. The private access modifiers is also known as native access modifiers .


If a method or variable is marked as protected or has the protected access modifier assigned to it; it can be accessed from classes of same package or sub classes of that class. This modifier is less restricted from private but more restricted from public access. Usually we use this keyword to make sure the class variables are accessible only to the subclasses. The protected access modifiers is also known as inherited access modifiers .


default - is not a keyword but public, private, protected are keywords. The default members or members with 'no access modifier' are visible within the package. And they are inherited to only sub classes which reside in the same package. That means they are not inherited and visible outside the package. Using 'default' is also sometimes referred as 'package-private' or 'no modifier' or 'friendly' access.

In the above example, the total field in the Bill class has no access modifier, which means that it is implicitly assigned the default access modifier.


We can access public methods or variables from all class of same package or other package. The public members of any class is accessible any where in program inside the same class and outside of the class, within the same package and outside of the package. public are also called universal access modifiers .

NOTE : If method have not any access modifier then we can access it inside all class of same package only.

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" . More about.... top level class be private or protected

Can we define private and protected modifiers for variables in interfaces?

Interface is like a blueprint of any class, where you declare your members. Any class that implement that interface is responsible for its definition. Having private or protected members in an interface doesn't make sense conceptually. More... private and protected modifiers for variables in interfaces?
 (C) 2022    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About