Java

Difference Between Protected and Private Access Modifiers in Java

Java allows the developers to make use of multiple functionalities as per the requirements. In such a case, the “Access Modifiers” come into effect that assists the programmers to maintain the scope of the member functionalities in the class. It is such that the accumulated functionalities can be assigned in accordance with the confidential or security requirements effectively.

This blog will elaborate on the differences between the Java “protected” and “private” access modifiers.

What is the “protected” Keyword in Java?

The “protected” keyword corresponds to an access modifier utilized for attributes, methods, etc. that allows them to be accessible in the same package and subclasses. The functionalities i.e., variable, function defined as “protected” can be accessed from the same class, subclasses of the same packages, different classes of the same packages, or the subclasses of different packages.

Example 1: Applying the “protected” Access Modifier in Java

This example utilizes the “protected” access modifier to allocate the member variable and function of the class as “protected”:

class Template{
 protected String name = "Linuxhint";
 protected String data(String lang) {
 return lang;
}}
class Child extends Template{
 String city = "Los Angeles";
}  
public class PrivProt{
 public static void main(String[] args) {
  Child object = new Child();
  System.out.println(object.name);
  System.out.println(object.data("Java"));
}}

 
In the above code snippet:

    • Firstly, create a class named “Template”.
    • In its definition, allocate the defined class variable and the function(returning the passed string) as “protected”.
    • Now, define a child class named “Child” inheriting the parent class “Template” using the “extends” keyword.
    • This class accumulates the stated string value.
    • Now, in the “main()” method, create an object of the “Child” class using the “new” keyword and the “Child()” constructor.
    • After that, invoke the protected member variable and function of the parent class via the child class(subclass) object.

Output


In this outcome, it can be analyzed that the “protected” members from the parent class are invoked accordingly with the help of the child class object.

What is the “private” Keyword in Java

The “private” keyword refers to the access modifier that allows the member functionalities to be accessed by the same class only and cannot be accessed from outside the class.

Example 2: Applying the “private” Access Modifier in Java

In this example, the “private” access modifier can be applied to limit the scope of the class members within the class only:

class Template{
 private String name = "Linuxhint";
 private String data(String lang) {
 return lang;
}}
class Child extends Template{
 String city = "Los Angeles";
}  
public class PrivProt{
 public static void main(String[] args) {
  Child object = new Child();
  System.out.println(object.name);
  System.out.println(object.data("Java"));
}}

 
According to the above code lines, perform the below-provided steps:

    • First of all, define a class named “Template”.
    • In its(class) definition, allocate the member variable and function of the class as “private”, respectively.
    • In the next step, likewise, create a child class inheriting the parent class i.e., “Template”.
    • In the “main()” method, similarly, create a child class object and invoke the allocated “private” variable and function, respectively.

Output


Here, it can be observed that the allocated “private” members cannot be accessed from outside the class due to their limited scope.

Core Differences Between “protected” and “private” Access Modifiers in Java

Following are some of the distinguishable differences between both the discussed access modifiers:

Protected Private
The utilized keyword is “protected”. The used keyword is “private”.
The “protected” members can be invoked from outside the class. The “private” members, however, cannot be invoked/accessed from outside the class.
The allocated “protected” members can be utilized in the same package subclass. The assigned “private” members cannot be utilized in the subclass of the same package.
The “protected” members can be utilized in different package subclass. The “private” members cannot be utilized in different package subclass.

 
However, the similarity between both the access modifiers is that both of these cannot be utilized in different package non-subclass.

Conclusion

The methods or data members declared as “protected” can be accessed from outside the class whereas they cannot be invoked from outside the class if assigned as “private”. This blog highlighted the differences between the Java “protected” and “private” access modifiers.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.