Abstraction in C# using an example
- By Gurunatha Dogi in C#
- Apr 17th, 2013
- 175289
- 48
Definition
Abstraction is one of the principle of object oriented programming. It is used to display only necessary and essential features of an object to ouside the world.Means displaying what is necessary and encapsulate the unnecessary things to outside the world.Hiding can be achieved by using "private" access modifiers.
Note - Outside the world means when we use reference of object then it will show only necessary methods and properties and hide methods which are not necessary.
Implementation of Abstraction
To implement abstraction let's take an example of a car. We knows a car, Car is made of name of car, color of car, steering, gear, rear view mirror, brakes, silencer, exhaust system, diesal engine, car battery, car engine and other internal machine details etc.
Now lets think in terms of Car rider or a person who is riding a car. So to drive a car what a car rider should know from above category before he starts a car driving.
Necessary things means compulsary to know before starting a car
1. Name of Car
2. Color of Car
3. Steering
4. Rear View Mirror
5. Brakes
6. Gear
Unnecessary things means not that compulsary to know for a Car rider
1. Internal Details of a Car
2. Car Engine
3. Diesal Engine
4. Exhaust System
5. Silencer
Now above same thing let me put in the coding style using C#
public class Car private string _nameofcar = "My Car"; private string _colorofcar = "Red"; public string NameofCar set _nameofcar = value; } get return _nameofcar; } } public string ColorofCar set _colorofcar = value; } get return _colorofcar; } } public void Steering() Console.WriteLine("Streering of Car"); } public void RearViewMirror() Console.WriteLine("RearViewMirror of Car"); } public void Brakes() Console.WriteLine("Brakes of Car"); } public void Gear() Console.WriteLine("Gear of Car"); } private void InternalDetailsofCar() Console.WriteLine("InternalDetailsofCar of Car"); } private void CarEngine() Console.WriteLine("CarEngine of Car"); } private void DiesalEngine() Console.WriteLine("DiesalEngine of Car"); } private void ExhaustSystem() Console.WriteLine("ExhaustSystem of Car"); } private void Silencer() Console.WriteLine("Silencer of Car"); } }
As you can see from above code that necessary methods and properties exposed by using "public" access modifier and unnecessary methods and properties (not compulsary) hidden by using "private" access modifier.
As you see to achieve abstraction we used access modifier "public" to expose some methods and properties to outside the class or world.
As you see to achieve abstraction we used access modifier "private" to hide some methods and properties from outside the class or world.
Finally lets check by creating an object of above class "Car" in our main program of a console application and by using object lets see weather we are getting all exposed necessary methods and properties.
class Program static void Main(string[] args) Car objCar = new Car(); } }
Conclusion
Sucessfully we have exposed necessary methods and properties to outside the world or class. This is how we need to implement abstraction or we can achieve abstraction in our code or application.
Friends feel free to ask me any queries on abstraction in c#. Thank you
Gurunatha Dogi
Gurunatha Dogi is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more..... read more
Comments
By Mahesh Shisode on 2018-11-03
Hi Sir, I need some simple study material for C sharp .net which cover all necessary topics with example. If you have your PDF related to complete C # please share it with me. thanks Mahesh ShisodeBy Afroj on 2016-05-28
want to know more about abstract class and abstract function...By Bhagwan Binay Thakur on 2016-04-23
Sir I want to know all access specifiers implementation is an example of Encapsulation or abastarction. I am very confused.By Vignesh on 2016-04-22
NiceBy Nilesh Yewale on 2016-04-10
dear sir plz send me complete example of abstract , interface , encapsulation classBy Venkatesh mahendru on 2016-03-02
So, what is encapsulation then if we can hide things in abstraction.By Vijay Kumar Prajapati on 2016-02-08
Excellent Sir......Thanks a lots!By Shobana on 2015-11-28
why we are using.. private access specifier in our program.. means Unnecessary things in our code??????By Velavan on 2015-11-23
very good explanation friend,i am really got an whole concept and understand all the things about the abstractionBy Lingaraj behera on 2015-10-16
i want know about oops concepts.By Venkatprasad on 2015-08-06
what is the difference between encapsulation and abstractionBy Ram on 2015-04-15
Very good real time exampleBy Manojbritto on 2015-01-31
nice explanation thank u very muchBy Amol on 2015-01-20
nice blogBy Yogesh on 2014-10-21
Gurunatha Dogi Yr Your Blog Is Realy Help Full For Me To Understand The Concept of Oops Again Thanks..By Ram Gosavi on 2014-09-29
Thanks Friend, Sala Kidhar bhi Interview dene jao, yeh bludi question puchta hi hai, theory to malum thi, but proper example chiye tha. Thank's once again mitra.By Rizwan Shahzad on 2014-09-23
Its very good explained oop concepts very help full , thanksBy Vijay Gujar on 2014-09-21
very nice article sir....thanks for that..By Sakshi on 2014-09-07
explain the exact diff between abstraction and encapsulation.By Vinoth on 2014-09-02
excellentBy Anup Dey on 2014-08-06
Thanks a lot !!By Hari krishna on 2014-07-15
Excellent Code.. Easy Way To UnderstandBy Rajendra on 2014-07-10
Thanks a lot....!!! Really nice explanation..now onwards threre wil be no confusion regarding abstraction and encapsulation .really nice thanks once againBy Bhanu Pratap singh on 2014-07-08
Nice example thanks broBy Hendrawan Haryanto on 2014-05-23
Very clear explanation.. Wow thank u so much..By Srikanth on 2014-05-23
Very nice explanation, thank uBy Devineni on 2014-05-17
Very nice explanationBy Sushant on 2014-04-23
thank u....By Mohd Ilyas on 2014-04-23
Excellent explanation,I was looking for similar explanation to implement to write into my blog. keep it up nice..By RAJKUMAR M on 2014-04-07
I was search about abstraction but i can't get correct explanation but in this blog its very very nice to understand.. Thanks a lotBy Mahima on 2014-03-27
very nice explaination...thanksBy Joy on 2013-11-29
I was searching some real time good example. This article really helped me lot. Thanks.By Jacob on 2013-11-02
Nice description. Very useful. You have described the topic in simpler & practical way. You have good narrative capability. Go ahead. All the best.By Raju on 2013-10-29
Thank you very much for providing this Article please let me know ,if you update new on your blogBy Srinivas D V on 2013-10-22
Nice Example... Good workBy Ravindra on 2013-10-17
You explain @ very zero level i never seen such type of example for abstraction. Thank you very much .I really like the way u explain it.By NAMOHAR M on 2013-10-16
thank u....By Nitin Suri on 2013-10-01
very briefly and nicely explainedBy Venkatreddy on 2013-08-28
very nice explanation..By Neha on 2013-08-17
about abstractions must read itBy JUTURU DINESH on 2013-08-12
thank q , i understand it easily ..thanks a lotBy Akil on 2013-08-06
Then how we will implement the Encapsulation. Please waiting for your response.By Suman on 2013-08-02
Thank you :) nice example on abstraction, but you meant outside world is where you refer the references of an object in some other, say code, but again its within the same coding environment. I want to know How does Abstraction helps to achieve real world requirements?By Ravindra Pratap on 2013-08-01
Really awesome article sir... Thank u very muchBy Mahendra Singh on 2013-07-20
Sir, Please tell me difference between Abstraction and Encapsulation also give code of Encapsulation Implimentation.By Amruta on 2013-07-09
well explained..By Gopi reddy k on 2013-06-27
hi , i want to interview questionsBy Ramdas Ananddas Chavan on 2013-06-11
its very nice concept.and very help ful to me understnadAdd a Comment

- By Gurunatha Dogi
- Nov 19th, 2012
- 26907
- 48