Skip to content
Related Articles

Related Articles

Improve Article
Protected vs Private Access Modifiers in Java
  • Last Updated : 28 Apr, 2021

Access modifiers are those elements in code that determine the scope for that variable. As we know there are three access modifiers available namely public, protected, and private. Let us see the differences between Protected and Private access modifiers. 

Access Modifier 1: Protected

The methods or variables declared as protected are accessible within the same package or different packages. By using protected keywords, we can declare the methods/variables protected.

Syntax:



protected void method_name(){

......code goes here..........

}

Example:

Java




// Java Program to illustrate Protected Access Modifier 
  
// Importing input output classes
import java.io.*;
  
// Main class 
public class Main {
  
  // Input custom string 
  protected String name = "Geeks for Geeks";
    
  // Main driver method 
  public static void main(String[] args) {
      
    // Creating an object of Main class 
    Main obj1 = new Main();
      
    // Displaying the object content as created
    // above of Main class itself  
    System.out.println( obj1.name );
     
}
}
Output
Geeks for Geeks

Access Modifier 2: Private 

The methods or variables that are declared as private are accessible only within the class in which they are declared. By using private keyword we can set methods/variables private.

Syntax: 

private void method_name(){

......code goes here..........

}

Example:

Java




// Java Program to illustrate Private Access Modifier
  
// Importing input output classes
import java.io.*;
  
// Main class
public class Main {
  
    // Input custom string
    private String name = "Geeks for Geeks";
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating an object of Main class
        Main obj1 = new Main();
  
        // Displaying the object content as created
        // above of Main class itself
        System.out.println(obj1.name);
    }
}
Output
Geeks for Geeks

Now after having an understanding of the internal working of both of them let us come to conclude targeted major differences between these access modifiers. 

ProtectedPrivate
The keyword used is ‘protected.’The keyword used is ‘private.’
Protected can be used within the same classPrivate can be used within a same class
Protected can be used in the same package subclassPrivate can not be used in the same package subclass
Protected can be used in different package subclassPrivate can not be used in different package subclass
Protected can not be used in different package non-subclassPrivate can not be used in different package non-subclass

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :