- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Public vs Protected vs Package vs Private Access Modifiers in Java
Java has various levels of protection that allow precise control over the accessibility of member variables and methods within classes, subclasses, and packages. The access control mechanism works with the help of access modifiers such as public, protected, private and package. They define scope of a variable, class and method. We are going to understand the various access modifiers in Java.
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.
Private Access Modifier
When we declare a class member as private then, it becomes non accessible to any code outside its class, including its subclasses. We can achieve higher level of encapsulation by using private keyword.
A few examples of private access modifier −
private int mobNo = 982266; private double income = 35005.55;
Here, variables are declared as private.
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.
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.
Public vs Protected vs Package vs Private access modifiers
From the above discussion, we can conclude the following differences among these access modifiers −
Accessible Regions |
Public |
Protected |
Package |
Private |
---|---|---|---|---|
Within Same Class |
Yes |
Yes |
Yes |
Yes |
Within Same Package Subclass |
Yes |
Yes |
Yes |
No |
Within Same Package non-Subclass |
Yes |
Yes |
Yes |
No |
Other Package Subclass |
Yes |
Yes |
No |
No |
Other Package non-Subclass |
Yes |
No |
No |
No |
In the above table, ‘Yes’ represents that member with the specified access modifier is accessible to the given region and ‘No’ represents that it is not accessible.
Example 1
The following example illustrates the use of public, protected and package modifiers in Java.
class Pack { protected void prnt1() { // method declared as protected String msg1 = "I am accessing a protected method"; System.out.println(msg1); } public void prnt2() { // method declared as public String msg2 = "I am inside a public method"; System.out.println(msg2); } void prnt3() { // method declared as default String msg3 = "I am inside a default method"; System.out.println(msg3); } } public class ClassShow extends Pack { public static void main(String args[]) { // object of sub class ClassShow obj = new ClassShow(); // method calling through object of sub class obj.prnt1(); obj.prnt2(); obj.prnt3(); } }
Output
I am accessing a protected method I am inside a public method I am inside a default method
Example 2
The following example illustrates what if we declare a method as private in Java.
class Pack { private void prnt() { // declaring a method private String msg = "I am inside a private method"; System.out.print(msg); } } public class ClassShow extends Pack { public static void main(String args[]) { // object of child class ClassShow obj = new ClassShow(); // method calling through object of child class obj.prnt(); } }
Output
ClassShow.java:13: error: cannot find symbol obj.prnt(); ^ symbol: method prnt() location: variable obj of type ClassShow 1 error
Here we modified the code of previous example, we got a compile time error because we tried to access a private method in subclass.
Conclusion
The access modifiers determine how a member of a class or method can be accessed. They are attached to the members at the time of declaration. In this article, we have explained all access modifiers in detail with respective examples and also, discussed a few differences among them.