Lakhyadeep Sen’s Post

Encapsulation is another important idea in object oriented programming. It focuses on protecting the internal data of a class and allowing controlled access to it through methods. Things that became clear : • encapsulation means wrapping data and the methods that operate on that data inside a single unit (a class) • variables are usually declared private so they cannot be accessed directly from outside the class • getter and setter methods are used to read or modify those variables • this helps maintain control over how data is changed • it improves security and keeps the class structure cleaner A simple example helps illustrate the idea : class Book { private int pageNo; public void setData(int x) { if (x > 0) pageNo = x; } public int getData() { return pageNo; } } In this structure, the page number cannot be accessed directly from outside the class. It can only be modified through the provided methods. This approach helps prevent invalid data from entering the system and keeps the internal structure of the class protected. #java #oop #programming #learning #dsajourney

To view or add a comment, sign in

Explore content categories