What are access modifiers?
Access modifiers in Java control the visibility and accessibility of classes, methods, and variables. They help enforce encapsulation by specifying which parts of your code can access certain elements. Java provides four main access modifiers:
- public
- protected
- default (no modifier)
- private
1. Public #
Definition: The public access modifier allows the member (variable, method, or class) to be accessed from any other class or package.
Example:
// Public class
public class PublicClass {
// Public variable
public int number;
// Public method
public void display() {
System.out.println("Number: " + number);
}
}
// Another class in the same package or a different package
public class Main {
public static void main(String[] args) {
PublicClass pc = new PublicClass();
pc.number = 10;
pc.display(); // Accessible and outputs: Number: 10
}
}
In this example, both the PublicClass and its members (number and display) are public, meaning they can be accessed from any other class.
2. Protected #
Definition: The protected access modifier allows the member to be accessed within its own package and by subclasses (including subclasses in other packages).
Example:
// Superclass in package 'pkg1'
package pkg1;
public class SuperClass {
// Protected variable
protected int number;
// Protected method
protected void display() {
System.out.println("Number: " + number);
}
}
// Subclass in the same package
package pkg1;
public class SubClass extends SuperClass {
void show() {
// Accessible because it's protected
number = 20;
// Accessible because it's protected
display();
}
}
// Class in another package
package pkg2;
import pkg1.SuperClass;
public class AnotherClass extends SuperClass {
public void test() {
// Accessible because it's protected and we are in a subclass
number = 30;
// Accessible because it's protected and we are in a subclass
display();
}
}
In this example, number and display are protected, allowing access within the same package and by subclasses.
3. Default (No Modifier) #
Definition: The default access modifier (when no modifier is specified) allows access only within the same package. It is also known as package-private.
Example:
// Class with default access in package 'pkg1'
package pkg1;
class DefaultClass {
// Default variable
int number;
// Default method
void display() {
System.out.println("Number: " + number);
}
}
// Class in the same package
package pkg1;
public class Main {
public static void main(String[] args) {
DefaultClass dc = new DefaultClass();
dc.number = 40;
dc.display(); // Accessible and outputs: Number: 40
}
}
In this example, DefaultClass and its members are accessible only within the same package because no access modifier is specified.
4. Private #
Definition: The private access modifier restricts access to the member within the same class only. It is the most restrictive access level.
Example:
// Class with private members
public class PrivateClass {
// Private variable
private int number;
// Private method
private void display() {
System.out.println("Number: " + number);
}
// Public method to access private members
public void setNumber(int num) {
number = num;
}
public void show() {
display(); // Accessible within the same class
}
}
public class Main {
public static void main(String[] args) {
PrivateClass pc = new PrivateClass();
pc.setNumber(50);
pc.show(); // Accessible and outputs: Number: 50
// The following lines would cause compile-time errors
// Error: number has private access in PrivateClass
// pc.number = 60;
// Error: display() has private access in PrivateClass
// pc.display();
}
}
In this example, number and display are private, so they cannot be accessed directly from outside the PrivateClass. Access is provided through public methods.
Summary #
- public: Accessible from any other class or package.
- protected: Accessible within the same package and by subclasses.
- Default (no modifier): Accessible only within the same package.
- private: Accessible only within the same class.
Access modifiers help control the visibility of classes, methods, and variables, promoting encapsulation and protecting the internal state of objects.