Difference between Encapsulation and Abstraction
Encapsulation
Abstraction
Example (Console Program)
class CustomerClass
{
public void AddCustomer(string name)
{
validate("uttam");
savedata("uttam");
}
private bool validate(string name)
{
//Validate name
return true;
}
private bool savedata(string name)
{
//Save to database
return true;
}
}
class Program
{
static void Main(string[] args)
{
CustomerClass p = new CustomerClass();
p.AddCustomer("uttam");
}
}
Where is Encapsulation and Abstraction in the above example?
Encapsulation: When we make function validate and savedata access modifier to private then we are doing encapsulation.
Abstraction: We are allowing function AddCustomer to be accessed from outside world and hiding the internal process like validate and savedata is abstraction.
- reduce complexity
- hide the information which you don't want to share with outside world
In simple word encapsulation is information hiding
Abstraction
- Show only the essential features
- hide internal process
In simple word abstraction is implementation hiding
Example (Console Program)
class CustomerClass
{
public void AddCustomer(string name)
{
validate("uttam");
savedata("uttam");
}
private bool validate(string name)
{
//Validate name
return true;
}
private bool savedata(string name)
{
//Save to database
return true;
}
}
class Program
{
static void Main(string[] args)
{
CustomerClass p = new CustomerClass();
p.AddCustomer("uttam");
}
}
Where is Encapsulation and Abstraction in the above example?
Encapsulation: When we make function validate and savedata access modifier to private then we are doing encapsulation.
Abstraction: We are allowing function AddCustomer to be accessed from outside world and hiding the internal process like validate and savedata is abstraction.
Comments
Post a Comment