Updated April 7, 2023
Definition on Abstraction vs Encapsulation in C#
Abstraction is one of the most important concepts of OOPs, it helps to hide the complexity from the user and helps to show only the important service to the user. We have Java programming language which makes use of Abstraction and able to hide the complicated and complex code from the user, we are only supposed to provide the services which are useful to the user, in the real world we have many explanations which can define the usage of Abstraction in an object-oriented programming language. Let’s taken a list of examples, ATM functionality, Phone calls, banking functionality provided to users, and many more. On the other hand, we have Encapsulation which is used for information hiding, it basically hides the behavior of the object from outside the world. It basically provides us the hiding layer to protect our data. In the coming section of the tutorial, we have closer look at the working of both these in detail for better understanding.
Head to Head Comparison Between Abstraction vs Encapsulation in C# (Infographics)
Below are the top 6 differences between Abstraction vs Encapsulation in C#:
Comparison Table of Abstraction vs Encapsulation in C#
Abstraction | Encapsulation |
It is the one of the most important concepts of object-oriented programming language, which helps at the designing level | It is also a concept of object-oriented programming language which helps us at the implementation level. |
Abstraction helps us to hide the complexity detail from the user, and only shows the features to the user | On the other hand, it provides us the information
Hiding helps to protect our data from external resources. |
Abstraction achieve at the class level, to create it we can use the ‘abstract’ keyword in C# | on the other hand, we have encapsulation which can be achieved by declaring the keyword as private and prevent them to be modified outside the class. |
It helps us to hide the unwanted data, and responsible to show only relevant features to the user, which means how we are doing particular things rather than the internal implementation | On the other hand, we have encapsulation will bind the data into a single unit and hide the code details from the user, which means how we are doing things internally and the user cannot modify the data so it basically prevents or data from alteration. |
To provide the implementation of abstract class we need to have one ore class that is not abstract and we can give a declaration to methods inside that class. | On the other hand, we have encapsulation which can be achieved by making the data members private and provide the getter and setter for them to access them outside the class. |
Ex: we can have a shape class, and other classes can use this class to provide the specific implementation. | Ex: inner implementation of Tv, phone, etc. we do not know how it works internally because hiding everything inside the single unit. |
Key differences of Abstraction vs Encapsulation in C#
As of now we already know that from the above table, how it works and how we can implement this in our program. In this section, we will have closer look at some example and few points which make the difference between them more clear. let’s get started to see below;
Abstraction
Suppose we go to ATM to withdraw money from it, at the start we only know how it works. But internally it processes many things like it uses servers, database, and many more things to process your data. It provides us so many functionalities which help us to perform the desired operation in less time. I want to withdraw some money so I will as a user will select the withdraw option and withdraw the money I want. But internally we do not know how it is working, so it is hiding from the user to keep it simple and provide them the necessary service they want.
In C# we can achieve abstraction by using the ‘abstract’ keyword this keyword can be used with class and method in C#, let’s take a closer look at both of this to see below;
a) abstract class: If we use the abstract keyword with class then it means we cannot instantiate that class, and if we want to access that class then it should be inherited by some other class in order to access it. For reference lets have a look at the syntax for using it see below;
Syntax:
abstract class Demo
{
// logic goes here ..//
}
As you can see we have defined one class which is abstract by using the abstract keyword. Now let’s taken a look at the abstract methods here.
b) Abstract method: If we define a method with an abstract keyword then it will be called an abstract method, which means we cannot provide its body in that class we have to inherit and provide its implementation. For reference let’s take and look at its syntax see below;
Syntax:
abstract class Demo
{
public abstract void m1();
}
In this way, we can define and implement abstraction in C#.
Encapsulation
If we talk about the encapsulation concept then we should understand it binds the data member and methods to a single unit, and protect them to get modified. Now we can simply achieve this by having the class member as private and we can define the public methods to access those variables. Let’s understand this by simple example see below;
Syntax:
public class Demo {
private String name;
private int rollno;
}
As you can see we have defined the private class member and after that, we can have getter and setter which will be public in order to access them. So it is very easy in C# to define the encapsulation by making use of getter and setter. So it is binding the object into a single unit which will prevent it from begin accessed from outside the world and hide the information and code also, complexity as well.
Conclusion
Both are the key concept of object-oriented programing language and works in the same way in every other programming language as well. Only the difference in the syntax and way of use. By the use of abstraction and encapsulation, we can easily provide ease to the user and protect the data from alteration as well.
Recommended Articles
This is a guide to Abstraction vs Encapsulation in C#. Here we discuss the Abstraction vs Encapsulation in C#, key differences with infographics, and comparison table, respectively. You may also have a look at the following articles to learn more –