Encapsulation in Java: A Comprehensive Guide with Examples

Abu Talha
2 min readSep 2, 2023

--

Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It is the practice of restricting access to certain components of an object and controlling how these components can be modified. In Java, encapsulation is achieved through the use of access modifiers, namely public, private, protected, and the default (package-private) modifier. In this article, we will explore encapsulation in Java in a formal and professional manner, along with practical examples of each access modifier.

Understanding Encapsulation

Encapsulation helps in hiding the internal state of an object and exposing only the necessary functionalities. It promotes data integrity, security, and maintainability by preventing unauthorized access and modification of an object’s properties.

Access Modifiers in Java

Public Modifier:

The public modifier grants unrestricted access to a class, method, or variable from any other class or package.

public class Circle {
public double radius; // Public variable

public Circle(double radius) {
this.radius = radius;
}

public double calculateArea() {
return Math.PI * radius * radius;
}
}

Private Modifier:

  • The private modifier restricts access to the class members within the same class. It is the most restrictive access modifier.

public class Student {
private String name; // Private variable

public Student(String name) {
this.name = name;
}

// Getter method to access the private variable
public String getName() {
return name;
}
}

Protected Modifier:

  • The protected modifier allows access to class members within the same class, its subclasses, and other classes in the same package.

package university;

public class Professor {
protected String name; // Protected variable

public Professor(String name) {
this.name = name;
}
}

Default (Package-Private) Modifier:

  • When no access modifier is specified, it becomes package-private, allowing access only from within the same package.

package bookstore;

class Book {
String title; // Default (package-private) variable

Book(String title) {
this.title = title;
}
}

Benefits of Encapsulation

  1. Data Protection: Encapsulation safeguards the internal state of an object, preventing unintended modification.
  2. Controlled Access: It enables controlled access to an object’s properties through well-defined methods (getters and setters).
  3. Improved Maintenance: Encapsulation makes it easier to modify the internal implementation of a class without affecting its users.
  4. Security: Private variables and methods can hide sensitive information and operations

Encapsulation is a critical concept in Java and OOP in general. By using access modifiers like public, private, protected, and default, you can effectively control access to class members, promoting data integrity, security, and maintainability in your Java applications. Understanding when and how to use these modifiers is essential for writing robust and secure code.

--

--

Abu Talha

SQA Engineer | Security Researcher | Application Penetration Tester | Back-End Developer