SLIDE 1 / 5

1. The Constructor: Customs Gate of Invariants

An invariant is a domain rule that must NEVER be broken (e.g. grade 0-10, non-negative balance). The constructor is the customs officer: invalid arguments are replaced with a safe default and logged to the console. The object is always allocated, but never with broken data.

Parámetros nombre: "Ana" nota: 9 Aduana nota >= 0 && nota <= 10 ✓ OK Estudiante Vivo nombre: "Ana" nota: 9 Estado Válido
Argument Inspection
new invocation: new Estudiante("Ana", 9);
Constructor Outcome: Instancia Creada en Heap

Grade satisfies the invariant (0 to 10). The object is safely instantiated in the Heap.

2. Constructor Overloading and this() Delegation

Multiple constructors shouldn’t duplicate validation logic. With this(...), auxiliary constructors delegate to a single canonical constructor. Crucial rule: this() must be the very first statement.

Libro(String tit) this(tit, "Anónimo"); Libro(String tit, String aut) this(tit, aut, 0.0); CONSTRUCTOR CANÓNICO Libro(tit, aut, precio) if (precio < 0) precio = 0; this.titulo = tit; this.autor = aut; this.precio = precio;
Invocation Trace
Step 1: Libro("Quijote") invoca a this()
Step 2: Libro("Quijote", "Anónimo")
Final landing: Constructor Canónico (3 args)

Rules live in ONE single method. If price invariants change, you only maintain one place.

3. The 4 Access Modifiers Matrix

Java offers 4 concentric visibility scopes. The golden rule is Least Privilege: default to private, opening up only when strictly required by architecture.

Modificador Misma Clase Mismo Paquete Subclase Mundo private ✓ SÍ ✕ NO ✕ NO ✕ NO default ✓ SÍ ✓ SÍ ✕ NO ✕ NO protected ✓ SÍ ✓ SÍ ✓ SÍ ✕ NO public ✓ SÍ ✓ SÍ ✓ SÍ ✓ SÍ
Scope Explorer
Selected level: private (Máximo Blindaje)
External access: Bloqueado para todo lo que no sea la clase

Ideal for state fields. No external entity can read or write values directly.

4. The Reference Leak Threat

Marking a field private does NOT guarantee encapsulation if a getter exposes a direct reference to a mutable object (like an array or Date). Callers can corrupt your internal array behind your back.

HistorialMedico private String[] consultas; ["Gripe", "Control", "Rayos"] 3 Consultas Activas Código Externo h.getConsultas()[0] = null; ¡Ataque exitoso sin setter!
Shielding Simulator
Getter Implementation: return this.consultas;
Attack result: Position 0 overwritten with null

Exposing the internal reference completely broke encapsulation. The client corrupted the medical records.

5. Immutability: Flawless Encapsulation

An immutable class (like String, LocalDate, or Java 16+ records) defines private final fields with no setters. It is thread-safe by design since its internal state never mutates once created.

record Coordenada(x, y) final double x = 12.5; final double y = 48.2; 🔒 SIN SETTERS (Thread-Safe) Hilo A: Lee x=12.5 Hilo B: Lee y=48.2 Hilo C: Lee x=12.5
Immutability Benefits
Concurrency Safety: 100% Thread-Safe
Side Effects: Cero (Zero)
Map key usage: Ideal e Inalterable

Immutability eliminates entire classes of bugs at once: data races, accidental aliasing, and state corruption.