Public vs Protected Access Modifiers in Java


The public and protected 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 public and protected access modifiers in Java through example programs.

Access Modifiers in Java

Public Access Modifier

Java does not restrict the accessibility of public members. Anything declared public can be accessible everywhere means we can access them within the class as well as outside the class and also within the package as well as outside the package. You may have noticed that the main() method in Java is always defined as public so that the JVM that exists outside any scope of the current program can call it.

A few examples of public access modifier −

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

Here, variables are declared as public.

Example

The following example illustrates how package members works in Java.

class Pack {  
   public void prnt() { 
      // method declared as public 
      String msg = "I am inside a public method";
      System.out.print(msg);
   }  
}  
public class ClassShow extends Pack { 
   // public child class
   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 inside a public method

In the above code, the class ‘Pack’ is parent class of ‘ClassShow’. In the parent class, we have declared a public 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 public method ‘prnt()’. Here, the child class is also public.

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 data1 = 5;
protected double data2 = 5.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 = "Tutorials Point!!";
      System.out.print("Welcome to " + 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

Welcome to Tutorials Point!!

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()’.

Public vs Protected access modifiers

From the above discussion, we can conclude the following differences between Public and Private access modifiers −

Public

Protected

We need to use the keyword ‘public’ to specify that a member is made public.

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

We can define any class as public.

A class can’t be defined as protected.

The public members are accessible to any class within the package as well as outside the package.

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.

It is applicable to both top level and member level.

It is applicable to the member level only.

Conclusion

We started this article by defining public and protected access modifiers and in later sections, we have explained them in detail with respective examples. In the end, we have discussed a few differences between them.

Updated on: 16-May-2023

233 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements