Protected vs Package Access Modifiers in Java


The protected and package 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. We know that these access modifiers play a major role in Java oops concepts like encapsulation, polymorphism and inheritance. It helps in preventing misuse of functionalities provided by a member. We will try to understand protected and package access modifiers in Java through example programs.

Access Modifiers in Java

Protected Access Modifier

It is mostly used in the case of inheritance to control the access of parent class members and corresponding child class members. It allow an element to be accessed outside the current package but only to the direct subclasses of classes. Here, packages are the containers that hold group of classes.

A few examples of protected access modifier −

protected int i1 = 108;
protected double d2 = 6.55;

Here, variables are declared as protected.

Example

The following example illustrates the use of 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

I am accessing a protected method

In the above code, the class ‘Pack’ is parent class of ‘ClassShow’. In the parent class, we have declared a protected method named ‘prnt()’ to print a simple message. In the main() method of child class, we have defined an object of child class ‘ClassShow’ to call the protected method ‘prnt()’.

Package Access Modifier

It is also known as the default 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.

A few examples of package access modifier −

int i1 = 108;
double d2 = 6.55;

Here, variables are declared as default or 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
   }
}

Output

Value of data1 and data2 multiplication is: 10

We have created two parameterized user defined methods named ‘methodA’ and ‘methodB’. In the main method, we have declared and initialized an integer variable ‘data1’ to 0 and then, passed it as an argument to the ‘methodA’. Now, the ‘methodA’ is called the ‘methodB’ with the incremented value of ‘data1’. Inside ‘methodB’ we have calculated multiplication and printed the result.

Protected vs Package access modifiers

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

Protected

Package

We use 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 to only inherited classes.

The package access modifiers is more restricted than protected as package 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 member level.

Conclusion

We started this article by defining protected and package access modifiers and in later sections, we have explained them in detail with respective examples. In the end, we have discussed how they are different from each other.

Updated on: 16-May-2023

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements