VOOZH about

URL: https://www.geeksforgeeks.org/java/protected-vs-private-access-modifiers-in-java/

⇱ Protected vs Private Access Modifiers in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Protected vs Private Access Modifiers in Java

Last Updated : 23 Jul, 2025

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. 

👁 Image

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:


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:


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
Comment
Article Tags: