Tutorials Home   >   Object-Oriented Programming (OOP)   >   Access Modifiers

Access Modifiers

Introduction to Access Modifiers

In object-oriented programming (OOP), we work with classes, objects, variables, and methods. While writing programs, it is very important to control who can access what inside a program. This is where access modifiers come in.

Access modifiers are keywords used in programming languages to control the visibility and accessibility of classes, variables, methods, and constructors. They help us decide:

  • Which parts of the program can use certain data

  • Which parts should be hidden for safety and clarity

Access modifiers are a key part of encapsulation, one of the four main principles of object-oriented programming.

Why Access Modifiers Are Important

Access modifiers help to:

  1. Protect data from unauthorized access

  2. Prevent accidental misuse of variables and methods

  3. Improve code readability and maintenance

  4. Make large programs easier to manage

  5. Support security and abstraction

Without access modifiers, all data would be open to everyone, which could lead to errors, bugs, and security issues.


2. Encapsulation and Access Control

Before understanding access modifiers in detail, we must understand encapsulation.

What Is Encapsulation?

Encapsulation means wrapping data and methods together into a single unit (class) and restricting direct access to some of the object’s components.

In simple words:

  • Sensitive data is hidden

  • Access is provided through controlled methods

Access modifiers are the main tools used to achieve encapsulation.

Real-Life Example

Think of a bank account:

  • Your balance is private

  • You can only access it using ATM or bank services

  • You cannot directly change the balance without proper rules

This is exactly how access modifiers work in programming.


3. Types of Access Modifiers

In Java, there are four main access modifiers:

  1. private

  2. default (no keyword)

  3. protected

  4. public

Each modifier defines a different level of access control.


4. Private Access Modifier

Definition

The private access modifier restricts access only within the same class.

  • Private members cannot be accessed outside the class

  • This is the most restrictive access level

Example

class Student {
private int age;
private void displayAge() {
System.out.println(age);
}
}

In this example:

  • age cannot be accessed from outside the Student class

  • displayAge() can only be called within the same class

Why Use Private?

  • To protect sensitive data

  • To prevent misuse

  • To enforce controlled access

Usually, private variables are accessed using getter and setter methods.

Getter and Setter Example

class Student {
private int age;
public int getAge() {
return age;
}

public void setAge(int a) {
age = a;
}
}

This way:

  • Data is protected

  • Access is controlled


5. Default (Package-Private) Access Modifier

Definition

When no access modifier is specified, Java uses default access.

  • Accessible within the same package

  • Not accessible outside the package

Example

class Teacher {
String subject; // default access
}

Here:

  • subject can be accessed by all classes in the same package

  • It cannot be accessed by classes in other packages

When to Use Default Access

  • When classes are closely related

  • When access is needed only inside the same package

Default access is useful for internal application logic.


6. Protected Access Modifier

Definition

The protected access modifier allows access:

  • Within the same package

  • In subclasses, even if they are in different packages

Example

class Animal {
protected void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void makeSound() {
sound();
}
}

In this example:

  • sound() is accessible in the subclass Dog

  • Even if Dog is in another package, access is allowed

Why Use Protected?

  • To support inheritance

  • To allow child classes to use parent class methods

  • To avoid exposing data to everyone

Protected is often used in frameworks and libraries.


7. Public Access Modifier

Definition

The public access modifier allows access from anywhere.

  • Accessible within the same class

  • Accessible within the same package

  • Accessible from other packages

Example

public class Car {
public void start() {
System.out.println("Car started");
}
}

Here:

  • The class Car and method start() can be accessed from any other class

When to Use Public

  • For methods that form the interface of a class

  • For functionality that should be available to users

  • For main methods and APIs

Public access should be used carefully, because it exposes data to everyone.


8. Comparison Table of Access Modifiers

Access Modifier Same Class Same Package Subclass Other Packages
private Yes No No No
default Yes Yes No No
protected Yes Yes Yes No
public Yes Yes Yes Yes

This table helps learners quickly understand the differences.


9. Access Modifiers with Classes

Class Level Access Modifiers

In Java:

  • Classes can be public or default

  • Classes cannot be private or protected at the top level

Example

public class Example {
}
class Example2 {
}
  • Example can be used anywhere

  • Example2 can only be used within the same package


10. Advantages of Using Access Modifiers

  1. Data Security

    • Protects sensitive information

  2. Better Code Organization

    • Clear separation of responsibilities

  3. Improved Maintainability

    • Changes affect fewer parts of the code

  4. Controlled Access

    • Only necessary methods are exposed

  5. Supports OOP Principles

    • Encapsulation and abstraction


11. Common Mistakes by Learners

  1. Making everything public

  2. Forgetting to use getters and setters

  3. Misunderstanding default access

  4. Using protected without inheritance

  5. Accessing private members directly

Avoiding these mistakes leads to better programming practices.


12. Summary

Access modifiers are an essential part of object-oriented programming. They control who can access what in a program and help make software secure, maintainable, and well-structured.

Key Points to Remember

  • private → most restricted

  • public → most open

  • protected → inheritance support

  • default → package-level access

  • Use access modifiers to protect data and control behavior