Access Modifiers

5
0

Introduction

Access modifiers in Java are keywords that control the visibility of classes, attributes, methods, and constructors. They determine which parts of a program can access a particular class member.

Java provides four access levels:

  • public :- Accessible everywhere (within the same class, same package, and outside the package).

  • private :- Accessible only within the class where it is declared.

  • protected :- Accessible within the same package and by subclasses in other packages.

  • Default (No modifier) :- Accessible within the same package (package-private).

Why Are Access Modifiers Used?

Access modifiers help developers:

  • Protect important data from direct access

  • Implement encapsulation

  • Control how classes and members are used

  • Prevent unwanted modifications

  • Make programs easier to maintain

Types of Access Modifiers in Java

1. Public

The public access modifier can make the attributes and methods of a class accessible from anywhere in the program, including classes outside the package. For example, consider the following code snippet:

import java.util.*;
class Employee {
public String name; // Public attribute
public void displayName() { // Public method
System.out.println("Employee Name: " + name);
}
}
class Main {
public static void main(String[] args) {
Employee emp = new Employee();
emp.name = "Alice"; // Accessible globally
emp.displayName(); // Accessible globally
}
}

Here, the name attribute and the displayName() method are set to public so they can be accessed from outside the class (in the main method).

Public

Public

2. Private

The private access modifier can make the attributes and methods of a class accessible only within the class where they were declared. For example, consider the following code snippet:

import java.util.*;
class BankAccount {
private double balance; // Private attribute
// Getter to provide controlled access
public double getBalance() {
return balance;
}
// Public method to deposit money
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}
// Main Class
class Main {
public static void main(String[] args) {
// Creating an object
BankAccount acnt = new BankAccount();
System.out.println(acnt.balance); // Throws an error
System.out.println(acnt.getBalance());
}
}

Here, the balance attribute is set to private, so it can only be accessed from within the class (using the getBalance() method) and throws an error when accessed outside of the class.

Private

Private

3. Protected

The protected access modifier can make the attributes and methods of a class accessible within the same package and in subclasses (even if they are in different packages). For example, consider the following code snippet:

import java.util.*;
class Vehicle {
protected String type; // Protected attribute
protected void displayType() { // Protected method
System.out.println("Vehicle Type: " + type);
}
}
class Car extends Vehicle {
public Car() {
this.type = "Car"; // Accessible in the subclass
}
}

Here, the subclasses can inherit and use the type attribute and displayType() method.

Protected

Protected

4. Default (No modifier)

When there is no access modifier specified, by default, the member is package-private in Java. This means that the member is accessible only in the package in which the class is declared, and nowhere else.

Consider the following code snippet:

import java.util.*;
class PackageDemo {
void showMessage() { // Default access
System.out.println("Default access in the same package.");
}
}
public class Main {
public static void main(String[] args) {
PackageDemo demo = new PackageDemo();
demo.showMessage(); // Accessible because it's in the same package
}
}
Default

Default

Java Access Modifiers Table

Modifier

Same Class

Same Package

Subclass Outside Package

Everywhere

public

Yes

Yes

Yes

Yes

protected

Yes

Yes

Yes

No

Default (No modifier)

Yes

Yes

No

No

private

Yes

No

No

No

Summary

Access modifiers in Java are keywords that control the visibility and accessibility of classes and their members. Java provides four levels: public, private, protected, and default (package-private), each allowing access in different scopes.

They help protect data, implement encapsulation, and control how classes and members are used. public provides the widest access, while private provides the most restricted access, with protected and default offering intermediate levels.

CS Core

Read Similar Blogs

Comments0