- Understand access modifiers and their importance
- Learn the four modifiers: public, private, protected, default
- Understand scope and visibility rules
Introduction to Access Modifiers in Java
Access modifiers are keywords that determine who can see and use your classes, methods, and variables. They're fundamental to encapsulation—one of OOP's core principles.
What are Access Modifiers?
Access modifiers control visibility of class members (fields, methods, constructors) from other classes. Java gives you four options:
1. Public (public)
The public modifier makes a class member accessible from anywhere in the program, including from different packages.
public class Car {
public String model; // Accessible from anywhere
public void start() { // Accessible from anywhere
System.out.println("Car started");
}
}
2. Private (private)
The private modifier restricts access to within the same class only. Private members cannot be accessed from outside the class.
public class BankAccount {
private double balance; // Only accessible within this class
private void updateBalance(double amount) {
this.balance += amount;
}
}
3. Protected (protected)
The protected modifier allows access within the same package and by subclasses (even in different packages).
public class Vehicle {
protected String brand; // Accessible in same package and subclasses
protected void service() {
System.out.println("Vehicle serviced");
}
}
4. Default (Package-Private)
When no access modifier is specified, it has package-private access. Members are accessible only within the same package.
public class Calculator {
int result; // Package-private access
void reset() { // Package-private access
result = 0;
}
}
Access Modifier Scope
| Modifier | Same Class | Same Package | Subclass (Different Package) | Everywhere |
|---|---|---|---|---|
public |
✅ | ✅ | ✅ | ✅ |
protected |
✅ | ✅ | ✅ | ❌ |
| default | ✅ | ✅ | ❌ | ❌ |
private |
✅ | ❌ | ❌ | ❌ |
Class-Level Access Modifiers
Classes can only be public or package-private (default). Inner classes can use all four modifiers.
public class PublicClass { } // Accessible everywhere
class DefaultClass { } // Package-private
public class OuterClass {
public class PublicInner { }
private class PrivateInner { }
protected class ProtectedInner { }
class DefaultInner { }
}
Why Use Access Modifiers?
- Encapsulation: Hide internal implementation details
- Security: Prevent unauthorized access to sensitive data
- Maintainability: Changes to private members don't affect other classes
- API Design: Control what clients can access
Common Mistakes
- Making all members
public(breaks encapsulation) - Using
protectedwhenprivatewould suffice - Forgetting that default access doesn't work across packages
- Making constructors
privatewithout providing factory methods
Access modifiers let you control visibility and implement proper encapsulation. The key is choosing the most restrictive modifier that still lets your code work—start with private and only open up as needed.
