Difference between private, protected, public and
package modifier or Keywords in Java

An access modifier restricts the access of a class, constructor, data member, and method in another class. you may use the public, private, and protected keywords while practicing java programs, these are called access modifiers.

In java we have four access modifiers: 1. default 2. private 3. protected 4. public

1. Default access modifier

  • When we define class, variables, or method without any modifier then it called as default access modifier.
  • The scope of this modifier is limited to the package only.
  • This means we have a class with the default access modifier in a package, only those classes that are in this package can access this class.
  • No other class outside this package can access this class.
  • Similarly, if we have a default method or data member in a class, it would not be visible in the class of another package.
 
package bala;

class Student {
int rollNumber;
String firstName;
String lastName;

Student(int rollNumber, String firstName, String lastName)
{
this.rollNumber = rollNumber;
this.firstName =  firstName;
this.lastName =  lastName;
}
void disply()
{
System.out.println("Student details are: Name :"+firstName+lastName+ "  
RollNumber :"+rollNumber);
}

}

package example;
 
import bala.*;
public class StudentInfo {
   public static void main(String args[]){
	Student obj = new Student(101,"Anna","George");  //error
          
	obj.disply();
   }
}

It will give error  we are trying to access he default method in another package
 

2). private access modifier

  • private keyword or modifier in java can be applied to member field, method, or nested class in Java.
  • You can not use the private modifier on top-level class.
  • private variables, methods, and classes are only accessible on the class on which they are declared.
  • private is the highest form of Encapsulation Java API provides and should be used as much as possible.
  • It's best coding practice in Java to declare variable private by default.
  • a private method can only be called from the class where it has declared.

As per the Rules of method overriding in Java, a private method can not be overridden as well. the private keyword can also be applied to the constructor and if you make the constructor private you prevent it from being sub-classed.

 
class MathOperations{  
   private double num = 500;
   private int add(int a,int b){
	return a+a;
   }
}  
public class Calculator {
   public static void main(String args[]){  
	MathOperations mathOps = new MathOperations();  
	System.out.println(mathOps.num); 
	System.out.println(mathOps.add(153,148));
   }  
}

It gives compile time Error.

 

3). protected access modifier

  • The difference between private and protected keyword is that Protected data members and methods are only accessible by the classes of the same package and the subclasses present in any package.
  • If you declare a variable protected means anyone can use it if they extend your class.
  • the top-level class can not make protected as well. Classes cannot be declared protected.
  • This access modifier is generally used in a parent-child relationship.
 

package bala;
public class MathOperations {

   protected int add(int a, int b){
	return a+b;
   }
}
 

package example;
import bala.*;
class Calculator extends MathOperations{
   public static void main(String args[]){
	MathOperations mathOps = new MathOperations();
	System.out.println(mathOps.add(153,148));
   }
}
Output:

301
 

In the above program, Calculator from example package is able to call the add() method, which is declared protected. This is because the Calculator class extends MathOperations class and the protected modifier allows the access of protected members in subclasses.

4). public access modifier

  • the public is an easy to access modifier in Java programming language and its bad practice to declare a field, method, or class by default public because once you make it public it's very difficult to make any change on the internal structure of the class as it affects all clients using it.
  • It is bad programming practice that to make all the fields of class as public if not needed.

Making class or instance variable public also violated the principle of Encapsulation which is not good at all and affects maintenance badly. Instead of making variable public, You should make it private and provided public getter and setter. the public modifier can also be applied to a top-level class. In Java name of the file must be the same as the public class declared in the file. Also, there should be at most one public class in java.

 
package bala;
public class MathOperations {
public int add(int a, int b){
	return a+b;
   }
}
T 

package example;
import bala.*;
class Calculator {
   public static void main(String args[]){
      MathOperations obj = new MathOperations();
      System.out.println(obj.add(23,123));
   }
}


output:
146
 

In the above example, the method adds () has a public modifier and class Calculator is able to access this method without even extending the MathOperations class. This is because the public modifier is accessible everywhere.

That's It. We have seen the difference between private, protected, public, and package modifier or keyword in Java.