<aside> ☕

L1.3 — Encapsulation & Constructors introduces the first core principle of OOP: encapsulation, covering how to protect data using access modifiers, implement getters/setters for controlled access, and create constructors to properly initialize objects.

</aside>

📋 At a Glance


1. What is Encapsulation?

Encapsulation is one of the three core principles of Object-Oriented Programming. It involves:

  1. Bundling data with methods - Combining related data (properties) and the methods that operate on that data into a single class
  2. Controlling access - Preventing direct access to internal data from external classes
  3. Providing clean interfaces - Offering controlled ways to interact with the object through public methods

The Problem Without Encapsulation

Consider this simple Person class:

public class Person {
    public String name;
    public int age;
    
    public void tell() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}