What is encapsulation?

Technical
Easy
Infosys
114.5K views

Specific question on one of the core OOPS principles.

Why Interviewers Ask This

Encapsulation is a fundamental concept in OOP. The interviewer wants to ensure the candidate understands how it protects data and enforces boundaries within classes. It is a basic but critical concept for writing secure and maintainable code.

How to Answer This Question

Define encapsulation as bundling data and methods that operate on that data within a single unit. Explain how access modifiers (private, public) restrict direct access to internal data. Provide a simple example, like a bank account class where balance is private. Highlight the benefit of data hiding.

Key Points to Cover

  • Data and method bundling
  • Access modifiers
  • Data hiding
  • Data integrity

Sample Answer

Encapsulation is the mechanism of wrapping data and methods that operate on that data into a single unit, typically a class. It restricts direct access to some of an object's components, preventing unintended interference and misuse. For example, in a BankAccount class, the balance might be private, and accessed only through public getter and setter methods. This ensures data integrity and allows for controlled modification of the internal state.

Common Mistakes to Avoid

  • Confusing with abstraction
  • Failing to mention access modifiers
  • No practical example provided

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 78 Technical questionsBrowse all 65 Infosys questions