Differences between Private, Public, Protected in Java

Differences between access modifiers

Ilknur Eren
Level Up Coding

--

Photo by Nick Wright on Unsplash

Private, Public, and Protected keywords are access modifiers. Depending on what you want to do with the variable or function, you might want to restrict the access modifier to be either public, private, and protected.

Below are quick summaries of each access modifier and where they can be accessed from. It’s important to know the differences between the access modifiers and use them in appropriate places.

Public

When a method, member, and classes are declared public, they can be accessed from anywhere. They can be accessed by class, package, subclass (same or different package), and the world.

public class example{
int add(int a, int b){
return a+b;
}
}

In the example above, the class example is a public class. This means that every other class can access this class and use the methods and variables inside of it. There is nothing stopping other classes from interacting with this class.

One benefit of public access modifiers is that they can be accessed easily. The negative of public classes is that we might change a variable from outside the class when we don’t need to.

Protected

Protected access modifiers allow the data members to be accessed by class, package, subclass (same package), subclass (different package). The difference between public and protected is that public can be accessed from outside class but protected cannot be accessed from outside class.

public class Addition{
protected int addTwo(int a, int b){
return a+b;
}
}
class Test extends Addition{
public static void main(String args[]){
Test obj = new Test();
}
}

In the example above, we created a protected method inside the Addition class. Later, we extended the Test class to use the Addition class. We can run this with no compile error. The protected method will transfer to the public class.

Private

Private access modifier only allows the data member to be accessed by class. Class and Interface cannot be declared as private.

class outside{
private int = 10;
private int square(int a){
return a*a;
}
}
public class inside{
public static void main(String args[]){
outside obj. = new outside();
}
}

In the example above, we are trying to create a new outside object in the inside class. When we run this code, we will get a compile-time error because the method inside-outside class is private, which means that other classes cannot access it.

It is best practice to use private access modifier when you are coding. This allows for more control of everything you are doing.

In summary, we have private, public and protected access modifiers available to use in Java. All of the access modifiers serve a different need when we code. One key thing to remember is to always try to use private if you can, next best access modifier is protected then the public is the last. It’s important to know the different access modifiers and how you can use them when you write code.

--

--