Access Modifiers

Thinking

Access modifiers in Java define the scope of accessibility for classes, methods, and variables. Understanding these is essential for encapsulation, security, and code organization.

They control how the members of a class can be accessed from other classes or packages.

Description

Java provides four types of access modifiers:

  • public: The member is accessible from any other class anywhere.
  • private: The member is accessible only within the class it is declared.
  • protected: The member is accessible within the same package and by subclasses even if they are in different packages.
  • default (package-private): No modifier specified means the member is accessible only within its own package.

Choosing the correct access modifier helps in protecting data and methods from unintended access.

Video Resources

Java Access Modifiers Tutorial

Clear explanation of public, private, protected, and default modifiers.

Examples (code)

Access Modifiers Example


// Example demonstrating access modifiers

package com.example;

public class AccessDemo {
    public int publicVar = 10;       // accessible everywhere
    private int privateVar = 20;     // accessible only in this class
    protected int protectedVar = 30; // accessible in package & subclasses
    int defaultVar = 40;             // accessible only in package

    public void show() {
        System.out.println("publicVar: " + publicVar);
        System.out.println("privateVar: " + privateVar);
        System.out.println("protectedVar: " + protectedVar);
        System.out.println("defaultVar: " + defaultVar);
    }
}

// In the same package, another class can access publicVar, protectedVar, and defaultVar,
// but NOT privateVar.
  

Access in Subclass Example


// In different package

package com.test;
import com.example.AccessDemo;

public class SubClass extends AccessDemo {
    public void display() {
        System.out.println(publicVar);       // Accessible
        // System.out.println(privateVar);   // Error: Not accessible
        System.out.println(protectedVar);    // Accessible in subclass
        // System.out.println(defaultVar);   // Error: Not accessible outside package
    }
}
  

Real-World Applications

Encapsulation & Security

Access modifiers are key to data hiding, preventing unauthorized access and modifications.

Modular Code Design

They help organize code into packages and modules with clear boundaries.

Inheritance Control

Control which members are inherited or visible to subclasses.

Interview Questions

Q1: What is the difference between public and private access modifiers?

Show Answer

public members are accessible from any other class, whereas private members are accessible only within their own class.

Q2: When would you use protected access?

Show Answer

Use protected when you want members accessible to subclasses and classes within the same package, but hidden from other classes.

Q3: What is default access modifier?

Show Answer

Default (package-private) access applies when no modifier is specified. Members are accessible only within the same package.