Difference between Public, Private and Protected modifier in Java?

In Java, you have got something called an access modifier, which specifies the accessibility of class, methods, and variables. There is four access modifier in Java namely public, private, protected and the default access modifier, also known as package level modifier. The difference between these access modifiers comes in their ability to restrict access to a class, method, or variables, public is the least restrictive access modifier while private is the most restrictive access modifier, package, and protected lies in between. 

Another key difference between public, protected, package and private modifier come from the point where you can apply them, for example, you cannot use private or protected modifier with a top-level class but you can use public modifier there.

The default or package modifier is a little bit special, when you do not specify any of the public, protected and private modifier, which is also the keywords, then Java automatically applies a default modifier (no it doesn't use default keyword), which means the said class, method or member will only be accessible inside the package it has declared.

Any class, which is outside the said package cannot access that element. The good thing about these concepts is that difference between public, protected, and private in Java is also one of the frequently asked Java interview questions.

Difference between the public, package, and private modifiers in Java

Apart from the fact that each access modifier allows members a specific level of access, there is some more subtle difference between them e.g. where exactly can you use them. Let's find out more about different access modifiers in Java.

1. One of the most important differences you need to remember related to access modifiers is the level of accessibility e.g. anything public is accessible to anywhere and anything private is only accessible the class they are declared. Similarly, anything default is accessible only inside the package and anything protected is accessible outside the package but only to child classes.

This means you can access public class, method, or variable from anywhere in the Java program and that's why key APIs are always public e.g. List interface, ArrayList class, etc but you might want to hide their implementation and you can do that by using private access modifier.

Why do you want to hide implementation? so that you can change it later without any worry that it will break any client. One of the best examples of hiding implementation is RegularEnumSet and JumboEnumSet classes which implement the EnumSet interface.

EnumSet is public and these two classes are hidden at the package level, which allows EnumSet to choose any of these depending upon key size of Enum. If the enum whose constants you are storing in EnumSet has less than 64 then it chooses RegularEnumSet which is more efficient.

If you are looking for a Java developer position and preparing for one then you can also take help from the wonderful book Java Interview Exposed by Wrox. It is one of the rare complete guides for a Java developer and tells which topic is important from interview point of view.

2. Second difference between public and modifiers is that they provide Encapsulation, which is just the opposite of accessibility. Encapsulation says hide anything which varies and that's why we hide implementations, public keyword provides lowest level of Encapsulation and private modifier provides highest level of Encapsulation in Java.

3. Third difference between private and public modifier is that you can use public modifier with top level class but you cannot make a top level class private in Java. Though both public and private modifier can be used with class e.g. nested and inner classes. You can make an inner class private which means no one can access it outside enclosing class.

Here is a nice table which compares access level provided by public, protected, private and default access modifier in Java.

Difference between public, private and protected in Java

As I said, use a public modifier to define API and use a private modifier to hide implementation, but you can also make constants public e.g. class variable which is also marked final and static.

Difference between default and protected modifier in Java

So, now you know the difference between private and public modifier in Java and understand when to use public and private in Java. Now, its time to understand difference between default or package level access and protected modifier in Java.

1) First difference between default and protected access modifier in Java is that default is nothing but package level accessibility i.e. if you don't provide any access modifier to a class, method or variable then Java by default make them accessible inside the package. 

If you cannot make a member private then your next best choice is package level access. That's why its also called package-private access. On the other hand, a protected modifier provides more accessibility than a default modifier. You can access a protected member outside the package, but only inside subclasses.

For example, if a method getVersion() is protected inside a class and they are inside build package and the class is public then this method is accessible to all classes inside the build package but only to sub classes outside the build package.

package build;
public class Builder{

protected int getVersion(){
   return 1;


and here is our subclass :

package gui;
public class Display extends Builder {

public void show(){
   //Ok, protected method can be called from subclass outside package


You can see that we can access getVersion() method which is protected, inside another package gui without any problem.

That's all about the difference between public, private, and protected modifiers in Java. Rule of thumb is to keep things as private as possible because that allows you flexibility to change them later. If you cannot make them private then at least keep them package-private, which is also the default access level for class, methods and variables in Java. 

If you like discussion in this interview questions, you would also like my explanation of other frequently asked Java interview questions as shown below :
  • Can we override the private method in Java? (answer)
  • Can we override the private method inside inner class in Java? (answer)
  • What is the difference between overloading and overriding in Java? (answer)
  • Difference between the type-1 and type-2 JDBC driver in Java? (answer)
  • Difference between == and equals() in Java? (answer)
  • What is the difference between String and StringBuffer in Java? (answer)
You should also keep in mind that access modifier can only be applied to members e.g. variables and methods which are part of the class, you can not make a local variable public, private or protected in Java.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.