← Back to libraryQuestion 6 of 273
JavaBeginner

Encapsulation (Data Hiding)

📖 Detailed Explanation:

Encapsulation is the practice of keeping fields private and providing access through public methods (getters/setters). This allows you to validate data before it is saved and prevents external classes from 'messing up' the internal state of an object. It is a cornerstone of maintainable, secure code.

🌍 Real-World Example:

A Smart Thermostat. You cannot reach inside and manually move the thermometer needle; you use the buttons (methods) to set a temperature, and the system validates that you don't set it to 500 degrees.

🎯 Scenario-Based Interview Question:

How does encapsulation improve the maintainability of a large-scale application? Provide a concrete example.