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(orisfor 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
- Control field access (read-only/write-only)
- Validate values
- Encapsulate implementation details