Access Modifiers :
- Controlling visibility and scope.
- Access modifiers define who can access classes, methods, and variables.
- They enforce encapsulation and data hiding.
- Four types: public, protected, default (package-private), and private.
Best Practices :
- Use private for fields, expose via getters/setters.
- Use public for methods meant to be reused.
- Use protected carefully in inheritance.
- Avoid default unless package-level access is intentional.
Access Modifiers on Instance/Object Variables& Static Variables:
private :
- Accessible only inside the same class.
- Cannot be accessed directly outside the class.Access through getter/setter Allowed.
- Used to achieve Encapsulation.
Ex : private int salary;
default (No Modifier) :
- Accessible only within the same package.
- Not accessible from outside the package.
- Also called package-private.
Ex : int salary;
protected :
- Accessible within the same package.
- Also accessible in subclasses (even in different packages).
- Mainly used in Inheritance.
Ex : protected int salary;
public :
- Accessible from anywhere.
- No restriction.
- Not recommended for sensitive data.
Ex : public int salary;
Real-World Best Practice :
- private instance variables + public getter/setter
class Employee {
private int salary; // private variable (hidden)
// Getter method (read value)
public int getSalary() {
return salary;
}
// Setter method (modify value)
public void setSalary(int salary) {
this.salary = salary;
}
}
Access Modifiers in Class :
public :
- Accessible from anywhere
- Can be used in any package
- File name must match the public class name
- Only one public class allowed per file
public class Employee {
void display() {
System.out.println("Public Class");
}
}
protected :
- No modifier written
- Accessible only within the same package
- Not accessible outside the package
class Employee {
void display() {
System.out.println("Default Class");
}
}
private & protected :
A top-level class cannot be private because private access means "only visible within the enclosing class". If a top-level class were private, it would not be visible anywhere else, making it useless for other parts of the program. However, inner (nested) classes can be declared as private.
Similarly, a top-level class cannot be protected because protected access is primarily related to inheritance and package visibility. It needs other classes (often subclasses in different packages) to interact with it, which is not possible if it's restricted to a protected scope at the top level. Inner classes can be protected.
Access Modifiers on methods:
private :
- Accessible only inside the same class.
- Cannot be accessed from outside.
private void calculateSalary() {
System.out.println("Salary Calculated");
}
default :
- No modifier written.
- Accessible only within the same package.
- Not accessible outside package.
void display() {
System.out.println("Default Method");
}
protected :
- Accessible in same package.
- Accessible in subclasses (even in different packages).
- Used mainly in inheritance.
protected void showDetails() {
System.out.println("Employee Details");
}
public :
- Accessible from anywhere.
- No restriction.
- Used for methods that should be available to other classes.
public void display() {
System.out.println("Public Method");
}
Access Modifiers on constructors :
private :
- Object cannot be created outside the class.
- Used to restrict object creation.
- Very important in Singleton pattern and utility classes.
- Object can only be created inside the same class.
class Company {
private Company() {
System.out.println("Private Constructor");
}
}
default :
- No modifier written.
- Object can be created only within same package.
- Not accessible outside package.
- Same package Allowed.
class Employee {
Employee() {
System.out.println("Employee Created");
}
}
protected :
- Accessible in same package.
- Accessible in subclasses (even in different packages).
- Used mostly in inheritance.
class Employee {
protected Employee() {
System.out.println("Protected Constructor");
}
}
class Manager extends Employee {
Manager() {
super(); // Allowed
}
}
public :
- Object can be created from anywhere.
- Most commonly used.
- Accessible from any package.
class Employee {
public Employee() {
System.out.println("Employee Created");
}
}

Top comments (0)