Differences Between Protected and Default Access Specifiers in Java



The protected and default access modifiers determine how a member of a class or method can be accessed. The modifiers are attached to the members at the time of declaration. Since Java follows the object-oriented programming paradigm, these access modifiers are used in encapsulation, polymorphism, and inheritance to control the behavior of class members.

We will try to understand the difference between protected and default access modifiers in Java through this article.

Protected Access Modifier

The protected access modifier is mostly used in the case of inheritance to control the access of parent class members and corresponding child class members. If a member of a class is declared protected, we can access it from all its subclasses and from the classes declared within the same package.

Example

The following example illustrates the use of a protected method in Java.

class Pack {  
   protected void prnt() {
      String msg = "I am accessing a protected method";
      System.out.print(msg);
   }  
}  
public class ClassShow extends Pack {  
   public static void main(String args[]) { 
      // creating object of child class
      ClassShow obj = new ClassShow();  
      // method calling through object of child class
      obj.prnt();  
   }  
} 

Output of the above code:

I am accessing a protected method

Default Access Modifier

It is also known as the package access modifier. When we don't specify any access specifier explicitly to classes and methods, the Java compiler automatically considers it as a default or package member. We can access these members inside sub classes as well as other classes within the same package.

Example

The following example illustrates how package members work in Java.

public class Overflw {
   static void methodA(int data1) {
      // default method 1
      data1++;
      methodB(data1); 
      // calling methodB
   }
   static void methodB(int data1) {
      // default method 2
      data1++;
      int data2 = 5; 
      // default member variable
      int mult = data1 * data2;
      System.out.println("Value of data1 and data2 multiplication is: " + mult);
   }
   public static void main(String []args) {
      int data1 = 0;  
      // variable with default modifier
      methodA(data1); 
      // calling methodA
   }
}

When you run the above code, it will show the following result -

Value of data1 and data2 multiplication is: 10

Protected vs Default Access Modifiers

From the above discussion, we can conclude the following differences between Protected and Package access modifiers:

Protected

Default

We use the protected keyword to specify that a member is made protected.

It does not require any specific keyword.

The protected members are accessible within the package as well as in other packages. But in the case of other packages, it is accessible only to inherited classes.

The default access modifier is more restricted than protected, as default members are accessible only to the same package.

We can't define a class as protected.

We can define a default class.

It is applicable to the member level only.

It is applicable to the top level as well as the member level.

Updated on: 2025-04-16T18:59:58+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements