Java Access Modifiers
Control who can see and use your code with access modifiers - the security guards of Java programming.
What are Access Modifiers?
Access modifiers in Java are keywords that set the accessibility (visibility) of classes, methods, and variables. They act like permission levels, deciding who can access your code.
Think of them like rooms in a house:
- Public - Like your living room, everyone can enter
- Private - Like your bedroom, only you can enter
- Protected - Like a family room, only family members can enter
- Default - Like a neighborhood park, only neighbors (same package) can enter
๐ก Why Use Access Modifiers? They help protect your code from being misused and make it more secure and organized!
Types of Access Modifiers
1. Public
The public modifier makes your code accessible from anywhere - any class, any package, anywhere in your project.
Use when: You want something to be available to everyone (like main methods, utility classes)
2. Private
The private modifier restricts access to within the same class only. No other class can see or use private members.
Use when: You want to hide internal details and protect data (most variables should be private)
3. Protected
The protected modifier allows access within the same package and by subclasses (child classes) even if they're in different packages.
Use when: You want child classes to inherit and use certain features
4. Default (No Modifier)
When you don't write any access modifier, Java applies the default access level. It's accessible only within the same package.
Use when: You want package-level privacy (less common)
Public Example
Here's how public works - anyone can access the method from anywhere:
Example: Public Access Modifier
public class Student {
public String name = "Rahul";
public void display() {
System.out.println("Name: " + name);
}
}
class Main {
public static void main(String[] args) {
Student s = new Student();
s.display(); // Accessible from anywhere
System.out.println(s.name); // Can access public variable
}
}
Name: Rahul Rahul
Explanation:
public String name- Variable accessible from any classpublic void display()- Method can be called from anywhere- Both the variable and method are freely accessible in the Main class
Private Example
With private, data is hidden and can only be accessed through public methods (getters/setters):
Example: Private Access Modifier
class BankAccount {
private double balance = 5000.0; // Hidden from outside
public double getBalance() { // Public method to access private data
return balance;
}
public void deposit(double amount) {
if(amount > 0) {
balance += amount;
}
}
}
class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
// System.out.println(account.balance); // ERROR! Can't access private
System.out.println("Balance: " + account.getBalance()); // Works!
account.deposit(1000);
System.out.println("New Balance: " + account.getBalance());
}
}
Balance: 5000.0 New Balance: 6000.0
Explanation:
private double balance- Cannot be accessed directly from outside the classgetBalance()anddeposit()- Public methods provide controlled access- This protects the balance from being modified incorrectly (data security!)
๐ Best Practice: Always make variables private and provide public getter/setter methods. This is called Encapsulation!
Protected Example
Protected members are accessible in subclasses, making them perfect for inheritance:
Example: Protected Access Modifier
class Animal {
protected String type = "Mammal"; // Accessible in child classes
protected void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
public void display() {
System.out.println("Type: " + type); // Can access protected member
sound(); // Can call protected method
System.out.println("Dog barks!");
}
}
class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.display();
}
}
Type: Mammal Animal makes sound Dog barks!
Explanation:
protected String type- Child class (Dog) can inherit and use this variableprotected void sound()- Child class can call this method- Protected allows sharing between parent and child classes while hiding from others
Quick Comparison Table
Here's a simple way to remember which modifier allows access where:
| Access Modifier | Same Class | Same Package | Subclass | Other Packages |
|---|---|---|---|---|
| Public | โ | โ | โ | โ |
| Protected | โ | โ | โ | โ |
| Default | โ | โ | โ | โ |
| Private | โ | โ | โ | โ |
Key Takeaways
- Use private for variables - Keep your data safe and controlled
- Use public for methods - When you want others to use your functionality
- Use protected for inheritance - When child classes need access
- Default is rarely used - Package-private access is less common
- Access modifiers = Security - They protect your code from misuse
๐ฏ Remember: Start with private by default, then make things public only when necessary. This keeps your code secure and well-organized!