Posts

Showing posts with the label Object-Oriented Programming (OOP)

Encapsulation in Java: Definition, Examples, and Advantages

Encapsulation in Java Encapsulation is an object-oriented programming (OOP) concept in which an object supports data binding and data hiding to protect its internal state. Key Concepts Data Binding Data binding is the process of associating the data with an object in memory. It ensures that the object's data is properly managed and accessed through the object. Data Hiding Data hiding is the process of restricting direct access to the object's data from other objects. Typically achieved by making variables private and providing getter and setter methods to access or modify the data. Encapsulation in Practice If a Java object supports both data binding and data hiding, it is considered an encapsulated object. Encapsulation protects the integrity of the data and allows controlled access. Example class Student {     // Private data (data hiding)     private String name;     private int age;     // Getter method     public String getName() { ...