Encapsulation is one of the 4 building blocks of OOPS. I have already written about other 3 OOPS concepts namely Inheritance, Polymorphism and Abstraction. In this blog post, we will discuss Encapsulation is OOPS.
What is dictionary meaning of Encapsulation
Encapsulation means "Enclose something in or as if in a capsule" (as per "oxforddictionaries.com" dictionary).
What is Encapsulation in OOPS
Having understood the meaning of Encapsulation, in general, let's understand what does it mean in the Object-Oriented programming world.
Image Courtesy: shutterstock
- Wikipedia says Encapsulation in OOPS is data hiding of the data members of the class like properties, fields etc. To implement this definition of data hiding you will make use of access specifiers.
- As per the other definition, Encapsulation is all about bundling the data with the methods which operate on that data.
In other words Encapsulation is about putting together methods/functions and data members all in one place called class so that users of those classes can easily make use of it.
Why we need Encapsulation
Having understood what is Encapsulation, now let's turn our attention into what is the advantage of it and what Encapsulation brings to the table in the object-oriented programming world.
With Encapsulation users of the classes need not worry about what how class provides its functionality. Similar to a real-world capsule. As a patient you really no need to know what is inside a capsule, that is something your physician worries about it and prescribe it to you.
As a user of the SmtpClient class in the .NET framework which sends a MailMessage to a recipient address, you don't need to know about how does it actually sends an email. The email sending logic is encapsulated inside the SmtpClient class (by an author of the class i.e., Microsoft)
No comments:
Post a Comment