An application is designed with private fields and public getter/setter methods. Which OOP principle does this exemplify?
Trap 1: Polymorphism
Polymorphism allows one interface to be used for different data types.
Trap 2: Inheritance
Inheritance is about deriving classes from a parent.
Trap 3: Abstraction
Abstraction hides implementation details, but encapsulation is about protecting data.
- A
Polymorphism
Why wrong: Polymorphism allows one interface to be used for different data types.
- B
Inheritance
Why wrong: Inheritance is about deriving classes from a parent.
- C
Encapsulation
Encapsulation bundles data and methods within a class, restricting direct access to the private fields and exposing controlled access through public getter and setter methods. This satisfies the constraint of hiding internal state while providing a defined interface for interaction, preventing external code from corrupting the object’s data integrity.
- D
Abstraction
Why wrong: Abstraction hides implementation details, but encapsulation is about protecting data.