In this article we are going to discuss difference between Abstraction and Encapsulation in C#. There are many blogs, articles are available on the internet regarding it but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Abstraction and Encapsulation in C#.
Abstraction Introduction
Abstraction are the way to achieve abstraction in C#. Abstraction in C# is the process to hide the internal details and showing functionality only.
The main goal of abstraction is to reduce complexity and allow the programmer to focus on interactions at a high level.
How it Works
Abstraction in C# can be achieved using abstract classes and interfaces.
Example
If you observe the below code, we defined a Laptop class with required fields, properties, and methods with the public, and private access modifiers to achieve an abstraction functionality by hiding and exposing some of the methods and properties based on our requirements.
using System;
using System.Text;
namespace Tutlane
{
public class Laptop
{
private string brand;
private string model;
public string Brand
{
get { return brand; }
set { brand = value; }
}
public string Model
{
get { return model; }
set { model = value; }
}
public void LaptopCompanyDetails()
{
Console.WriteLine("Brand: " + Brand);
Console.WriteLine("Model: " + Model);
}
public void LaptopKeyboard()
{
Console.WriteLine("Type using Keyword");
}
private void MotherBoardInfo()
{
Console.WriteLine("MotheBoard Information");
}
}
class Program
{
static void Main(string[] args)
{
Laptop objLap = new Laptop();
objLap.Brand = "Lenovo";
objLap.Model = "Model-14";
objLap.LaptopCompanyDetails();
objLap.LaptopKeyboard();
}
}
}
Output
Encapsulation Introduction
The process of binding or grouping the state(Data Members) and behaviour(Data Functions) together into a single unit(class, interface, struct) is called Encapsulation in C#.
The main goal of encapsulation is to protect the internal state of an object and to control how the data is accessed and modified.
How it Works
Encapsulation is achieved using access modifiers (private, protected, internal, public) to restrict access to the fields of a class. Properties are used to provide controlled access to these fields.
Example
public class Person
{
// Private fields
private string name;
// Public property for name
public string Name
{
get { return name; }
set { name = value; }
}
}
// Usage
class Program
{
static void Main()
{
Person person = new Person();
person.Name = "John";
Console.WriteLine($"Name: {person.Name}"); // Output: Name: John
}
}
S.No. | Abstraction | Encapsulation |
---|---|---|
1 | Abstraction solves the problem in the design level or interface level. | Encapsulation solves the problem in the implementation level. |
2 | Abstraction is used for hiding the unwanted data and giving only relevant data. | Encapsulation is hiding the code and data into a single unit to protect the data from outer world. |
3 | Abstraction is set focus on the object instead of how it does it. | Encapsulation means hiding the internal details or mechanics of how an object does something. |
4 | We can implement abstraction using abstract class and interfaces. | Whereas encapsulation can be implemented using by access modifier i.e. private, protected and public. |
5 | Example - CAR – the driver of the car only needs to know how to drive it. Not how its engine and the gear box and other internal components work. | Example - A bank can be thought of as a fully encapsulated class that provides access to the customers through various methods (getters and setters). Rest of the data inside bank is hidden to protect from outside world. |