Java Class Design
What this topic covers
Build classes with encapsulation, inheritance, and polymorphism, and apply static members, immutability, and singleton patterns. Override Object methods consistently so identity works correctly in hash-based collections.
Why do immutable classes need private final fields and defensive copies?
Private final fields cannot be reassigned from outside the class, so the object's state stays fixed after construction. A final class also cannot be subclassed to add mutable behavior. When a constructor receives a mutable collection or date, copy it first, so later changes by the caller do not reach the object.