Access Modifiers in Java
In Java, access modifiers control the visibility and accessibility of classes, methods, and fields. There are four types of access modifiers in Java:
- public
- private
- protected
- default (no modifier)
1. Public Access Modifier
The public access modifier makes a class, method, or field accessible from anywhere in the program. It has the widest scope and can be accessed by any other class, regardless of the package it belongs to.
Example 1: Using the public Access Modifier
In this example, the class MyClass and the method displayMessage() are declared public. This means they can be accessed from any other class:
public class MyClass {
// Public method
public void displayMessage() {
System.out.println("Hello, this is a public method!");
}
}
We can call the displayMessage() method from any other class:
public class Main {
public static void main(String[] args) {
// Create an object of MyClass
MyClass myObject = new MyClass();
// Call the public method
myObject.displayMessage();
}
}
Output:
Hello, this is a public method!
2. Private Access Modifier
The private access modifier restricts access to the class, method, or field to only within the class where it is declared. It is the most restrictive access modifier.
Example 2: Using the private Access Modifier
In this example, the field num and the method setNum() are declared private. They cannot be accessed outside of the MyClass class:
public class MyClass {
// Private field
private int num;
// Private method
private void setNum(int num) {
this.num = num;
}
// Public method to access the private field
public void displayNum() {
System.out.println("Number: " + num);
}
}
We cannot access num or setNum() directly from outside the class. However, we can use a public method like displayNum() to access the private fields indirectly:
public class Main {
public static void main(String[] args) {
// Create an object of MyClass
MyClass myObject = new MyClass();
// We cannot directly access setNum() or num here
// myObject.setNum(10); // This would cause a compile-time error
// Use a public method to display the private field
myObject.displayNum();
}
}
Output:
Number: 0
3. Protected Access Modifier
The protected access modifier allows access within the same package and also to subclasses (even if they are in different packages). This is less restrictive than private but more restrictive than public.
Example 3: Using the protected Access Modifier
In this example, the field num is declared protected, so it can be accessed within the same package or by subclasses:
public class MyClass {
// Protected field
protected int num;
// Protected method
protected void setNum(int num) {
this.num = num;
}
// Public method to display the protected field
public void displayNum() {
System.out.println("Number: " + num);
}
}
Accessing protected Members in a Subclass
In the following code, we create a subclass of MyClass and access the protected field num:
public class SubClass extends MyClass {
public void setNumInSubClass(int num) {
// Accessing protected field from superclass
this.num = num;
}
}
In this case, the SubClass can directly access the num field because it is declared protected in the superclass.
Example: Using the SubClass
public class Main {
public static void main(String[] args) {
// Create an object of SubClass
SubClass myObject = new SubClass();
// Set the protected field using subclass method
myObject.setNumInSubClass(100);
// Display the value of protected field
myObject.displayNum();
}
}
Output:
Number: 100
4. Default Access Modifier
If no access modifier is specified, Java uses the default access modifier. The default access modifier allows access only within the same package.
Example 4: Using the Default Access Modifier
In this example, no access modifier is specified, so the field num has default access:
public class MyClass {
// Default access field
int num;
// Default access method
void setNum(int num) {
this.num = num;
}
// Public method to display the default access field
public void displayNum() {
System.out.println("Number: " + num);
}
}
We can access the default access field num only within the same package:
public class Main {
public static void main(String[] args) {
// Create an object of MyClass
MyClass myObject = new MyClass();
// Set the default access field
myObject.setNum(200);
// Display the value of the field
myObject.displayNum();
}
}
Output:
Number: 200
5. Comparison of Access Modifiers
| Access Modifier | Class | Package | Subclass | World |
|---|---|---|---|---|
| public | Yes | Yes | Yes | Yes |
| private | Yes | No | No | No |
| protected | Yes | Yes | Yes (if subclass) | No |
| default | Yes | Yes | No | No |
6. Conclusion
Access modifiers in Java play a crucial role in controlling the visibility of classes, methods, and fields. By using public, private, protected, and default access modifiers appropriately, you can implement proper encapsulation, improving the security and maintainability of your code.