OOP of Object-Oriented Design (Study Notes)
“Encapsulation involves wrapping data (fields) and methods in a single unit (class) and restricting direct access using access modifiers.”
Encapsulation is the concept of bundling data as attributes and logic as methods, and then putting them together in a single unit called an object. The Object’s internal state is hidden from the outside world, and access to the data or state is controlled through well-defined interfaces available as public methods.
Purpose & Benefits:
Examples:
Minimal Example:
Realistic Example:
Define your classes
: Identify objects in your requirements, think about the data they hold, and the functionality they support. For the Person class, the data includes name and age, and the functionality includes accessing and modifying these attributes.Enforce encapsulation
: Declare the class’s data members (attributes) as private to restrict direct access from outside the class. Provide public methods (getters and setters) to access and modify those attributes.Use access modifiers
: The private access modifier restricts direct access to the attributes outside the class. Only methods in the class have access to these private mem-bers. Public methods are the interface through which external code interacts with the
integrity.
object’s attributes, hiding internal implementation details and maintaining the object’sEncapsulation is particularly useful in the following scenarios:
Protecting data integrity
: When you need to ensure an object’s data remains consistent and valid. For example, in the Person class, encapsulation hides name and age as private attributes, allowing the setAge ( method to enforce rules like non-negative values, preventing invalid modifications, and ensuring the object’s state is reliable.Controlling access and improving security
: Encapsulation restricts direct access to sensitive data. While attributes like name and age in a lerson class are not righly sensitive, encapsulation becomes essential for classes handling critical information such as passwords. Although encapsulation alone does not guarantee full security, it acts as a foundational layer by limiting unwanted access.Modularity and reusability
: When designing classes that can be reused across different applications. The Person class’s clear interface makes it modular and reusable in contexts like school management or social networking systems.While encapsulation is powerful, avoid these common mistakes:
Over-encapsulation
: Creating excessive getter and setter methods for every attribute can make code verbose and harder to maintain.Under-encapsulation
: Failing to hide internal details can lead to tight coupling and reduced modularity. For instance, if name and age in the Person class were public, other parts of the code could modify them directly, leading to potential inconsistencies.