What does encapsulation help to achieve in Object-Oriented Programming?

202520232025

What does encapsulation help to achieve in Object-Oriented Programming?

  1. A.

    Code Reusability

  2. B.

    Code Optimization

  3. C.

    Security

  4. D.

    Code Compilation

Attempted by 4 students.

Show answer & explanation

Correct answer: C

Encapsulation is one of the four pillars of Object-Oriented Programming. It means bundling an object's data (attributes) and the methods that operate on that data into a single unit (a class), and exposing that data only through a controlled interface using access modifiers such as private, protected, and public.

By restricting direct access to an object's internal state, encapsulation prevents external code from accidentally or maliciously modifying data it should not touch. This controlled, protected access is exactly what security means in this context, so the correct answer is Security.

  • Code Reusability is achieved mainly through inheritance and polymorphism, which let one class reuse or extend another's behaviour, not through encapsulation's access control.

  • Code Optimization is about improving runtime performance or resource usage, handled by compilers, algorithms, and coding technique; encapsulation does not target performance.

  • Code Compilation is the process of translating source code into an executable form, governed by the language's compiler and syntax; it is unrelated to how a class restricts access to its own members.

Hence, encapsulation's defining benefit among the given options is Security, achieved through data hiding and controlled access.

Explore the full course: Hexaware Preparation