Skip to Content
bytegarden

Data Hiding

It separates an object’s internal implementation from its external interface. This concept is inspired by real-world engineering products.

Benefits of Data Hiding

  • Simplicity: Makes products easier to use

    • Users don’t need technical knowledge
    • Focus on operations rather than implementation
  • Protection: Prevents direct manipulation of internal components

    • Reduces risk of damage
    • Ensures proper usage
  • Abstraction: Separates what something does from how it does it

    • Users only need to know available operations
    • Implementation details are hidden

Getters

  • Start with get (or is for boolean)
  • Provide read-only access to fields
  • e.g., getLength()

Setters

  • Start with set
  • Provide write access to fields
  • e.g., setLength()

Benefits of Getters & Setters

  1. Control field access (read-only/write-only)
  2. Validate values
  3. Encapsulate implementation details