Protected vs Private Access Modifiers in Java Last Updated : 28 Apr, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 ); } } OutputGeeks 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); } } OutputGeeks 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. ProtectedPrivateThe keyword used is 'protected.'The keyword used is 'private.'Protected can be used within the same classPrivate can be used within a same classProtected can be used in the same package subclassPrivate can not be used in the same package subclassProtected can be used in different package subclassPrivate can not be used in different package subclassProtected can not be used in different package non-subclassPrivate can not be used in different package non-subclass Comment More infoAdvertise with us Next Article Protected vs Private Access Modifiers in Java S sravankumar_171fa07058 Follow Improve Article Tags : Java Difference Between Java-Modifier Practice Tags : Java Similar Reads Protected vs Package Access Modifiers in Java Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by usi 4 min read Private vs Protected vs Final Access Modifier in Java Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by usi 5 min read Public vs Private Access Modifiers in Java Whenever we are writing our classes we have to provide some information about our classes to the JVM like whether this class can be accessible from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not etc. we can specify this information by usi 3 min read Protected vs Final Access Modifier in Java Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by usi 5 min read Package vs Private Access Modifiers in Java Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by usi 3 min read Like