HI WELCOME TO KANSIRIS

Abstraction in C# with Real-Time Examples

Leave a Comment

In this article, I am going to discuss the Abstraction in C# with some examples. Please read our previous article before proceeding to this article where we discussed Encapsulation in C# with examples. The Abstraction in C# is one of the fundamental OOPs principles which acts as a supporting principle. That means Abstraction principle in C# makes sure that all other three principles (Encapsulation, Polymorphism, and Inheritance) are working together to give the final shape of the project.

What is Abstraction in C#?
The process of defining a class by providing the necessary and essential details of an object to the outside world and hiding the unnecessary things is called abstraction in C#. It means we need to display what is necessary and compulsory and need to hide the unnecessary things to the outside world. In C# we can hide the member of a class by using private access modifiers.

Let us understand Abstraction with a real-time example.

Let us understand this with a car example. As we know a car is made of many things, such as the name of the car, the color of the car, gear, breaks, steering, silencer, diesel engine, the battery of the car, engine of the car, etc. Now you want to ride a car. So to ride a car what are things you should know.
The things a car driver should know are as follows.
  1. Name of the Car
  2. The color of the Car
  3. Gear
  4. Break
  5. Steering
So these are the things that should be exposed and know by the car driver before riding the car.
The things which should be hidden to a Car rider as are follows
  1. The engine of the car
  2. Diesel Engine
  3. Silencer
So these are the things which should be hidden to a car driver.
Now let’s implement what we discussed with a program using C#
namespace AbstractionDemo
{
public class Car
{
private string _CarName = "Honda City";
private string _CarColur = "Black";
public string CarName
{
set
{
_CarName = value;
}
get
{
return _CarName;
}
}
public string CarColur
{
set
{
_CarColur = value;
}
get
{
return _CarColur;
}
}
public void Steering()
{
Console.WriteLine("Streering of the Car");
}
public void Brakes()
{
Console.WriteLine("Brakes of the Car");
}
public void Gear()
{
Console.WriteLine("Gear of the Car");
}
private void CarEngine()
{
Console.WriteLine("Engine of the Car");
}
private void DiesalEngine()
{
Console.WriteLine("DiesalEngine of the Car");
}
private void Silencer()
{
Console.WriteLine("Silencer of the Car");
}
}
}
As shown in the above example, you can see that the necessary methods and properties are exposed by using the “public” access modifier whereas the unnecessary methods and properties hidden from outside the world by using the “private” access modifier as shown in the below image.
Abstraction in C# - Exposing the class members
As you can see in the above image, the methods and properties which we want to expose to the outside world are created using the public access specifier. Now from outside the class, we can create the object of this Car class and can access the above methods and properties that we will see after a while. Have a look at the following image.
Abstraction in C# - Hiding the class members
As shown in the above image, the methods and variables which don’t want to expose to the outside world are created using the private access modifier. Now from outside the class, we can create the instance of Car class but we cannot access the above methods and variables.
Consuming the Car Class:
Let us create an instance of the Car class within the Main method of the Program class and then let’s try to invoke the public and private members of the Car class.
public class Program
{
public static void Main()
{
//Creating an instance of Car
Car CarObject = new Car();
//Accessing the Public Properties and methods
string CarName = CarObject.CarName;
string CarColur = CarObject.CarColur;
CarObject.Brakes();
CarObject.Gear();
CarObject.Steering();
//Try to access the private variables and methods
//Compiler Error, 'Car._CarName' is inaccessible due to its protection level
CarObject._CarName;
//Compiler Error, 'Car.CarEngine' is inaccessible due to its protection level
CarObject.CarEngine();
}
}
As you can see, we can access the public members of the Car class using the Car instance. But while we are accessing the private members of the Car class using the same Car instance, we get Compiler Error. Hence, this proofs that, we have exposed the necessary methods and properties to outside the world or class by hiding the unnecessary members of the class by using the Abstraction in C#. 

What is the difference between Abstraction and Encapsulation in C#?

The Encapsulation is the process of hiding irrelevant data from the user or you can say Encapsulation is used to protect the data. For example, whenever we buy a mobile, we can never see how the internal circuit board works. We are also not interested to know how the digital signal converts into the analog signal and vice versa. So from a Mobile users point of view, these are some irrelevant information,. This is the reason why they are encapsulated inside a cabinet.
In C# programming, we will do the same thing. We will create a cabinet and keep all the irrelevant information which should not be exposed to the user of the class.
Coming to abstraction in C#, It is just the opposite of Encapsulation. What it means, it is a mechanism which will show only the relevant information to the user. If we consider the same mobile example. Whenever we buy a mobile phone, we can see and use many different types of functionalities such as a camera, calling function, recording function, mp3 player, multimedia etc. This is nothing but the example of abstraction in C#. The reason is we are only seeing the relevant information instead of their internal working.

Encapsulation vs Data Abstraction
  • Encapsulation is data hiding(information hiding) while Abstraction is detail hiding(implementation hiding).
  • While encapsulation groups together data and methods that act upon the data, data abstraction deals with exposing to the user and hiding the details of implementation.
Advantages of Abstraction

  • It reduces the complexity of viewing the things.
  • Avoids code duplication and increases reusability.
  • Helps to increase security of an application or program as only important details are provided to the user.
In the next article, I am going to discuss Inheritance in C# with examples. Here, in this article, I try to explain the Abstraction in C#. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.