Access modifiers

Comprehensive study notes, diagrams, and exam preparation for Access modifiers.

Access Modifiers

Definition

Access modifiers are keywords used in programming languages to specify the accessibility level of classes, constructors, methods, and data members. They determine who can access what within a program.

For example, in Java, the common access modifiers are:

  • private
  • default (package-private, when no modifier is written)
  • protected
  • public

These modifiers create a controlled boundary around data and behavior, which is one of the main principles of encapsulation.


Main Content

1. Types of Access Modifiers

Private Access Modifier

A private member can be accessed only within the same class where it is declared. It is the most restrictive access level and is mainly used to protect sensitive data.
Example:

  class Student {
      private int age;
  }

Here, age cannot be directly accessed outside the Student class. This is useful when data must be validated through methods like getters and setters.

Default Access Modifier (Package-Private)

When no access modifier is written, the member gets default access. It is accessible only within the same package. This is useful when classes are designed to work together inside a group but should not be exposed outside that group.
Example:

  class College {
      String name;   // default access
  }

This name variable can be accessed by other classes in the same package but not by classes from another package.

Protected Access Modifier

A protected member is accessible within the same package and also in subclasses outside the package. It is commonly used when a class wants to allow inheritance-based access while still maintaining some restriction.
Example:

  class Animal {
      protected void sound() {}
  }

A subclass of Animal can use sound() even if it is in a different package, which supports reuse in inheritance.

Public Access Modifier

A public member is accessible from anywhere in the program. It provides the least restriction and is used for methods or classes that are meant to be widely available.
Example:

  public class Car {
      public void start() {}
  }

Here, the class and its method can be accessed from any other class.

2. Access Level Control and Encapsulation

Hides internal implementation details

Access modifiers allow a class to hide its internal working and expose only necessary parts. This is a core idea in encapsulation. For instance, a bank account balance should usually be private, while methods to deposit or withdraw money may be public.

Protects data from unauthorized changes

If important variables are private, they cannot be changed directly from outside the class. This prevents invalid or harmful values. Example: a salary field in an employee class should not be directly modified without rules or validation.

Creates a clean interface

A class can expose only the methods needed by users and keep helper methods hidden. This makes the class easier to use and reduces confusion.

3. Practical Use in Classes, Methods, and Variables

Class-level access

Access modifiers can be applied to classes to control whether the class can be used in other parts of the program. In many languages like Java, top-level classes can be public or default, while nested classes may use other modifiers.

Method-level access

Methods may be public, private, protected, or default depending on whether they are meant for external use, internal use, or inheritance use.
Example:

  • public methods form the external API
  • private methods perform internal helper tasks
  • protected methods support subclass behavior

Variable-level access

Instance variables and static variables are often made private to prevent direct access. Public constants may be used when values are intended to remain fixed and globally readable.

Access Scope Illustration

Most restrictive ------------------------------------------------> Least restrictive

private  ->  default  ->  protected  ->  public

This progression shows how visibility increases from internal-only access to full program-wide access.


Working / Process

1. Declare the class member with an access modifier

Choose private, default, protected, or public depending on how widely the member should be visible.
Example: private int balance;

2. Use the member only within its permitted scope

The compiler checks whether the access is allowed. If code tries to access a restricted member from an unauthorized location, an error occurs.

3. Provide controlled access when needed

If direct access is restricted, expose safe methods such as getters, setters, or public service methods.
Example:

   class Account {
       private double balance;

       public double getBalance() {
           return balance;
       }

       public void deposit(double amount) {
           if (amount > 0) balance += amount;
       }
   }

In this example, balance cannot be changed directly, but it can be accessed and updated through controlled methods.


Advantages / Applications

Improves data security and protection

Sensitive information such as passwords, account balances, or internal state can be hidden from direct access.

Supports encapsulation and abstraction

Access modifiers make it possible to expose only necessary operations while hiding implementation details, which is central to object-oriented design.

Enhances maintainability and modularity

By controlling access, developers can change internal code without affecting outside users, as long as the public interface stays the same.

Useful in real-world software design

Access modifiers are used in banking systems, student management systems, libraries, games, and enterprise applications to control how objects interact.

Helps prevent programming errors

Restricting direct access reduces accidental modification of variables and encourages safer coding practices.


Summary

  • Access modifiers control the visibility of class members.
  • They are essential for protecting data and supporting encapsulation.
  • Common modifiers include private, default, protected, and public.
  • They help organize code and restrict access appropriately.
  • Important terms to remember: access modifier, encapsulation, visibility, private, protected, public, default, package-private