What is encapsulation?
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.
Related Interview Questions
Explain company process?
Easy
TCSDo you know Java? What are some of its key features?
Easy
TCSWhat is Object-Oriented Programming in Java?
Medium
GoogleHow does exception handling work in Java and what is the difference between throw and throws?
Medium
TCSWhat is the best way to find the middle of three numbers?
Easy
InfosysWrite Selenium and Java code to automate an Amazon search
Medium
Infosys