Access Modifiers in Java
Access modifiers control the visibility and accessibility of classes, methods, and fields.
Four Access Levels:
- private
- default (no modifier)
- protected
- public
1. Private Access
Accessible only within the same class.
class BankAccount {
private double balance; // Private field
private void updateBalance() { // Private method
// Only accessible within this class
}
public void deposit(double amount) {
balance += amount; // Can access private field
updateBalance(); // Can call private method
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
// account.balance = 1000; // ❌ Error: balance is private
// account.updateBalance(); // ❌ Error: method is private
account.deposit(1000); // ✓ OK: public method
}
}
2. Default Access (Package-Private)
Accessible within the same package only. No modifier keyword.
// File: Student.java (in package school)
package school;
class Student { // Default access
String name; // Default access
int rollNo; // Default access
void display() { // Default access
System.out.println(name + " - " + rollNo);
}
}
// File: Test.java (in same package 'school')
package school;
public class Test {
public static void main(String[] args) {
Student s = new Student(); // ✓ OK: same package
s.name = "John"; // ✓ OK: same package
s.display(); // ✓ OK: same package
}
}
// File: Other.java (in different package)
package other;
import school.Student;
public class Other {
public static void main(String[] args) {
Student s = new Student(); // ❌ Error: default access
}
}
3. Protected Access
Accessible within same package and by subclasses (even in different packages).
// File: Animal.java
package animals;
public class Animal {
protected String name; // Protected field
protected void eat() { // Protected method
System.out.println("Eating...");
}
}
// File: Dog.java (same package)
package animals;
public class Dog extends Animal {
void display() {
System.out.println(name); // ✓ OK: subclass
eat(); // ✓ OK: subclass
}
}
// File: Cat.java (different package)
package pets;
import animals.Animal;
public class Cat extends Animal {
void display() {
System.out.println(name); // ✓ OK: subclass
eat(); // ✓ OK: subclass
}
}
// File: Test.java (different package, not subclass)
package test;
import animals.Animal;
public class Test {
public static void main(String[] args) {
Animal a = new Animal();
// a.name = "Max"; // ❌ Error: not subclass
// a.eat(); // ❌ Error: not subclass
}
}
4. Public Access
Accessible from anywhere.
// File: Calculator.java
public class Calculator {
public int add(int a, int b) { // Public method
return a + b;
}
}
// File: Test.java (any package)
import Calculator;
public class Test {
public static void main(String[] args) {
Calculator calc = new Calculator();
int result = calc.add(10, 20); // ✓ OK: public
System.out.println(result);
}
}
Access Levels Summary
| Modifier | Same Class | Same Package | Subclass | Other Packages |
|---|---|---|---|---|
| private | ✓ | ✗ | ✗ | ✗ |
| default | ✓ | ✓ | ✗ | ✗ |
| protected | ✓ | ✓ | ✓ | ✗ |
| public | ✓ | ✓ | ✓ | ✓ |
Complete Example
public class Employee {
private String name; // Only this class
int id; // Same package (default)
protected double salary; // Same package + subclasses
public String department; // Everywhere
private void privateMethod() {
System.out.println("Private method");
}
void defaultMethod() {
System.out.println("Default method");
}
protected void protectedMethod() {
System.out.println("Protected method");
}
public void publicMethod() {
System.out.println("Public method");
privateMethod(); // Can access private in same class
}
}
class Test {
public static void main(String[] args) {
Employee emp = new Employee();
// emp.name = "John"; // ❌ Error: private
emp.id = 101; // ✓ OK: default (same package)
emp.salary = 50000; // ✓ OK: protected (same package)
emp.department = "IT"; // ✓ OK: public
// emp.privateMethod(); // ❌ Error: private
emp.defaultMethod(); // ✓ OK: default (same package)
emp.protectedMethod(); // ✓ OK: protected (same package)
emp.publicMethod(); // ✓ OK: public
}
}
Best Practices
1. Encapsulation:
class Student {
private String name; // Private fields
private int rollNo;
public String getName() { // Public getters
return name;
}
public void setName(String name) { // Public setters
this.name = name;
}
}
2. Class Access:
public class PublicClass { // Can be accessed anywhere
}
class DefaultClass { // Only within same package
}
Common Usage Patterns
Pattern 1: Private fields, Public methods
class BankAccount {
private double balance; // Hide data
public void deposit(double amount) { // Public interface
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
Pattern 2: Protected for inheritance
class Vehicle {
protected String brand; // Accessible to subclasses
protected void start() {
System.out.println("Vehicle started");
}
}
class Car extends Vehicle {
void display() {
System.out.println(brand); // Can access protected
start(); // Can call protected
}
}
Access Within Class
class Example {
private int privateField = 1;
int defaultField = 2;
protected int protectedField = 3;
public int publicField = 4;
void testAccess() {
// All accessible within same class
System.out.println(privateField); // ✓
System.out.println(defaultField); // ✓
System.out.println(protectedField); // ✓
System.out.println(publicField); // ✓
}
}
Quick Reference
class AccessExample {
private int a; // Same class only
int b; // Same package
protected int c; // Same package + subclasses
public int d; // Everywhere
}
Exam Tips
Remember:
- private: Same class only
- default: Same package only
- protected: Same package + subclasses
- public: Accessible everywhere
- Fields: Usually private
- Methods: Usually public
- Class: public or default
- Encapsulation: Private fields + Public methods
Common Questions:
- What are access modifiers?
- Difference between private, protected, public?
- What is default access?
- Where can protected be accessed?
- Why make fields private?
- What is encapsulation?