I came across an article on Titled “Encapsulation in Object Oriented Programming”. It Explores in detail about the core principles of object-oriented Programming (OOP): encapsulations. Although I had already heard about this concept in computer science class, that time just covered the minor details and definition without really understanding why it is so important in practice. Reading this article helped me see encapsulation in a new way, especially through real-world examples and comparisons that made the idea much easier to grasp.
Encapsulation is explained as hiding the internal details of a class and only allowing controlled access through methods. This is usually done by keeping variables private and providing getters and setters for interaction. According to the article, errors can easily occur when other program pieces change an object’s state directly in the absence of encapsulation. However, with encapsulation, updates are subject to predetermined guidelines, maintaining the system’s unity and security.
One of the examples that stood out to me compared encapsulation to locking a house. If all the doors and windows are left open, anyone can come in and move things around. In programming terms, that would be like exposing all fields of a class as public. By contrast, encapsulation is like keeping the house locked and only providing a few keys for safe entry. In the same way that you wouldn’t give full write access to everyone on a GitHub repository, you don’t want every part of a program to freely change the internal state of your objects. Instead, you provide limited access—like adding collaborators with specific permissions or using pull requests for controlled changes. This analogy helped me understand why developers stress the importance of data hiding: it keeps things organized, prevents misuse, and ensures that changes happen in a safe, predictable way.
The article provided an actual scenario using a bank account class. Any piece of code might set a balanced variable to a negative value if it is public, which would go against the program’s logic. However, the application may set restrictions like “balance cannot go below zero” if the balance is private and is only accessible via the deposit() or withdraw() methods. This demonstrated to me how encapsulation guarantees that critical rules are always observed while also hiding information.
What I learned from the article is that encapsulation goes beyond coding style—it’s about safeguarding data and ensuring programs behave correctly. A helpful way to picture it is how GitHub handles collaboration. Instead of letting everyone edit the main project directly, contributors usually fork the repository, make changes in their own copy, and then open a pull request for review. This process keeps the main branch stable while still allowing improvements, like what we practiced in our CS343 and CS348 classes at WSU.
Overall, the article provides the details we need to practice and build stronger knowledge in the basics of the field.
References:
“Encapsulation in Object Oriented Programming” by AKshay Raut (Medium) https://akshayraut.medium.com/encapsulation-in-object-oriented-programming-4cfb0ee28f12
FreeCodeCamp. (n.d.). The Four Pillars of Object-Oriented Programming. https://www.freecodecamp.org/news/four-pillars-of-object-oriented-programming/
From the blog CS@Worcester – Pre-Learner —> A Blog Introduction by Aksh Patel and used with permission of the author. All other rights reserved by the author.