Abstraction and Encapsulation

Encapsulation protects abstraction.

Spread the love

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.

Let’s protect your computers
You can visit Computer Repair Service

Leave a Reply

Your email address will not be published. Required fields are marked *