Selecting the right access modifiers in Java is generally very important if you want to maintain data security and achieve proper encapsulation in Java. Each Java Access Modifiers define the different level of accessibility that typically influences how the classes, methods, and variables are able to interact within or across different packages. There are generally four types of access modifiers in Java: public, protected, private, and default.
In this blog, we will learn the difference between public, protected, package-private, and private access modifiers in Java with the help of examples that will help you to create robust and maintainable Java Programs:
Table of Contents:
What are Access Modifiers in Java?
Access modifiers define the visibility and accessibility of classes, methods, and variables. They help in data protection and encapsulation by controlling who can access variables and methods in a class.
Modifier 1: Public Access Modifiers in Java
In Java, the public access modifier allows a class, method, or variable to be accessible from anywhere in the program. It is used when we want to access the variables or methods throughout the program. It is declared using the public keyword, and used for global access to the variables and methods.
Example:
Output:
Explanation: In the above code, we have two classes Library and User, in which Library is a public class that includes a public variable bookName, and a public method displayBook(). As these are public, we can access both the variable and method inside our main() function directly.
- System.out.println(lib.bookName): Here we are directly accessing the variable of the class Library.
- lib.displayBook(): Here we are accessing the method of the Library class.
Modifier 2: Protected Access Modifier in Java
The protected access modifier in Java allows a variable, method, or constructor to be accessible within the same package and by the child class of the outside package. It is declared with the protected keyword. It is used to achieve Inheritance.
Example:
Output:
Explanation: In the above code, we have created a Parent class and inherited a Child class from it. Inside the main() method, we have called the display() method using three different objects:
- Using a Parent class reference (Parent p = new Parent();): This directly calls the display() method of the Parent class.
- Using a Child class reference (Child c = new Child();): Since Child inherits from Parent, it can access the display() method.
- Using a Parent class reference but a Child class object (Parent p1 = new Child();): This demonstrates polymorphism, where the method of the parent class is called using a child class instance.
Since display() is declared protected, it can be accessed within the same package or through inheritance in another package.
Modifier 3: Private Access Modifiers in Java
In Java, the private access modifier is used to restrict access to a variable or method only within the same class. It ensures data security and prevents direct access from other classes. It is declared using the private keyword. We can not access the private variable or method from outside the class. Private access modifiers are used to achieve encapsulation and data hiding.
Example:
Output:
Explanation: In the above code, we have made the display() function private, which will only be accessible inside the class, but we are trying to call the private display() method inside the main(), which will cause a compilation error. Because we can not access a private method outside the class.
Modifier 4: Default (Package-Private) Access Modifier in Java
In Java, when no access modifier (such as public, protected, or private) is specified, the member or class gets package-private (default) access. This means it is only accessible within the same package but not from outside the package, even if inherited. This access modifier works like public in the same package and acts as private outside the package.
Example:
Output:
Explanation: In the above code, we have declared a package named mypackage. Inside this package, we have declared a class DefaultExample with the default (package-private) access modifier. Since no explicit access modifier (public, protected, or private) is used, this class is only accessible within the same package. Since Test and DefaultExample are in the same package, Test can access DefaultExample directly. The method showMessage() is also declared with default access, meaning it can be accessed only within the same package. And it prints the output message “This is a package-private method”.
Public vs Protected vs Package vs Private Access Modifier in Java
Here are the following differences between public, protected, package, and private Access Modifier in Java:
Modifier | Within Same Class | Within Same Package | Subclass in Another Package | Outside Package (Non-Subclass) |
public | Yes | Yes | Yes | Yes |
protected | Yes | Yes | Yes (through inheritance) | No |
default (package-private) | Yes | Yes | No | No |
private | Yes | No | No | No |
Conclusion
So far in this blog, we have learned different types of access modifiers like: public, private, protected, and package-private in Java. These modifiers are used to control the visibility and accessibility of classes, methods, and variables in Java.
- Public variables and methods are accessible everywhere (inside the same class, package, different packages, and subclasses).
- Protected variables and methods are accessible within the same package and in subclasses (even in different packages).
- Private variables and methods are accessible only within the same class (not even in subclasses).
- Default (package-private) variables and methods are accessible only within the same package (not in subclasses outside the package).
If you want to excel in your career in Java, you may refer to our Java course.
Public vs Protected vs Package vs Private Access Modifier in Java – FAQs
1. What are the 4 access modifiers in Java?
The four access modifiers in Java are public, protected, private, and default (package-private). They control the accessibility of classes, methods, and variables.
2. Can we declare a constructor as private?
Yes, we can declare a constructor as private in Java. This means that the object of that class cannot be created from outside the class.
3. Can static methods be private?
Yes, static methods can be private, which means they can only be accessed within the same class.
4. Can abstract methods be private?
No, abstract methods cannot be private because they must be overridden in subclasses.
5. Are constructors public or private?
Constructors can be public, private, protected, or default, depending on the program’s needs.