Encapsulation:
With the definition, it says the binding of the data members and member function into a single unit is encapsulation. So when we define a class, then through encapsulation a class can hide the internal details of how an object does something.
Anytime you declare a class/structure you can specify its members (variables, properties, methods) accessibility to the users.
Encapsulation simplifies the interaction between objects. One object can use another object without knowing all its data and how it looks.
Going for an example, let us suppose that a client named BOB is there now he wants to store the values and then give his desired output which we don’t know.
BOB has an object might with id, Full name, Email, company, and Mobile properties.
Now he wants to give info to some of the banks.
If a Bank object wants to use a BOB object, it can request the Fullname and Mobile for the bank without needing to know the other details like (email , company etc) of the BOB object.
Performing encapsulation,
A class can change the internal implementation without hurting the overall functionality of the system.
class Bob
{
private int Id { get; set; }
private string Fullname { get; set; }
private string bank()
{
return “id:- ” + id+ ” and FullNameis:- ” + Fullname ;
}
}
Bob _bob = new Bob();
string s =_bob.bank();
Console.WriteLine(s);
**So here we see bank does not know what all details is locked in the class bob but he gets the result , and in future he can collect the mail too, for which we dont need to create an another class .This is encapsuling the class.
Encapsulation protects abstraction.
WHAT IS ABSTRACTION?
In short -Hiding the implementation is called abstraction, which is like a similar term like encapsulation.
**
So yeah! Abstraction and Encapsulation are confusing terms and dependent on each other. Let’s take it by an example:
class Bob
{
private int Id { get; set; }
private string Fullname { get; set; }
private string Mobile{ get; set; }
private string bank()
{
return “id:- ” + id+ ” and FullNameis:- ” + Fullname ;
}
}
When you created bob class, you did encapsulation by writing properties and functions together(Id, Fullname etc)You perform abstraction when you expose this class to client as
Person p = new Person();
p.CustomName();
Now if, your client wants to know the mail as well, then without disturbing the function call. You do encapsulation by adding one more property into Person class like this.
public class Person
{
private int Id { get; set; }
private string Name { get; set; }
private string Mobile{ get; set; }
private string Mail{ get; set; }
private string bank()
{
return “id:- ” + id+ “FullNameis:- ” + Fullname ” + and Mail is:- ” + Mail;
}
}
Look, even after adding an extra property in the class, your bank doesn’t know what you did to your code. This is where you did abstraction.
Thank you! I hope you have got it.
Mumbai, often referred to as the financial capital of India, is a hub for businesses and industries. With countless corporate…
When it comes to security services in Noida, there are several reputable companies that cater to various needs. Let me…
Are you tired of dealing with tooth decay, gum problems, and expensive dental treatments? Look no further! Steel Bite Pro…
In the world of fine jewelry, oval moissanite rings have emerged as a captivating choice for those seeking elegance and…
Noida, located in the state of Uttar Pradesh, is a fast-growing hub of digital marketing agencies in India. Choosing the…
As we move into 2023, the world of motorcycles in India is set to see some exciting changes. Manufacturers are…
Leave a Comment