Abstraction in C# using an example



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
                _nameofcar = value;
                return _nameofcar;

        public string ColorofCar
                _colorofcar = value;
                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();



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






More C# Articles To Read Next:


What are Delegates in C# - Step By Step Creating and Using the Delegate


Views - 18906 |Category - C#


Difference Between For Loop And For Each Loop in Csharp


Views - 22911 |Category - C#

Author: 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, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory




Posted by Afroj on 2016-05-28

want to know more about abstract class and abstract function...

Posted 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.

Posted by Vignesh on 2016-04-22


Posted by Nilesh Yewale on 2016-04-10

dear sir plz send me complete example of abstract , interface , encapsulation class

Posted by Venkatesh mahendru on 2016-03-02

So, what is encapsulation then if we can hide things in abstraction.

Posted by Vijay Kumar Prajapati on 2016-02-08

Excellent Sir......Thanks a lots!

Posted by Shobana on 2015-11-28

why we are using.. private access specifier in our program.. means Unnecessary things in our code??????

Posted by Velavan on 2015-11-23

very good explanation friend,i am really got an whole concept and understand all the things about the abstraction

Posted by Lingaraj behera on 2015-10-16

i want know about oops concepts.

Posted by Venkatprasad on 2015-08-06

what is the difference between encapsulation and abstraction

Posted by Ram on 2015-04-15

Very good real time example

Posted by Manojbritto on 2015-01-31

nice explanation thank u very much

Posted by Amol on 2015-01-20

nice blog

Posted by Yogesh on 2014-10-21

Gurunatha Dogi Yr Your Blog Is Realy Help Full For Me To Understand The Concept of Oops Again Thanks..

Posted 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.

Posted by Rizwan Shahzad on 2014-09-23

Its very good explained oop concepts very help full , thanks

Posted by Vijay Gujar on 2014-09-21

very nice article sir....thanks for that..

Posted by Sakshi on 2014-09-07

explain the exact diff between abstraction and encapsulation.

Posted by Vinoth on 2014-09-02


Posted by Anup Dey on 2014-08-06

Thanks a lot !!

Posted by Hari krishna on 2014-07-15

Excellent Code.. Easy Way To Understand

Posted by 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 again

Posted by Bhanu Pratap singh on 2014-07-08

Nice example thanks bro

Posted by Hendrawan Haryanto on 2014-05-23

Very clear explanation.. Wow thank u so much..

Posted by Srikanth on 2014-05-23

Very nice explanation, thank u

Posted by Devineni on 2014-05-17

Very nice explanation

Posted by Sushant on 2014-04-23

thank u....

Posted 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..

Posted 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 lot

Posted by Mahima on 2014-03-27

very nice explaination...thanks

Posted by Joy on 2013-11-29

I was searching some real time good example. This article really helped me lot. Thanks.

Posted 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.

Posted by Raju on 2013-10-29

Thank you very much for providing this Article please let me know ,if you update new on your blog

Posted by Srinivas D V on 2013-10-22

Nice Example... Good work

Posted by 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.

Posted by NAMOHAR M on 2013-10-16

thank u....

Posted by Nitin Suri on 2013-10-01

very briefly and nicely explained

Posted by Venkatreddy on 2013-08-28

very nice explanation..

Posted by Neha on 2013-08-17

about abstractions must read it

Posted by JUTURU DINESH on 2013-08-12

thank q , i understand it easily ..thanks a lot

Posted by Akil on 2013-08-06

Then how we will implement the Encapsulation. Please waiting for your response.

Posted 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?

Posted by Ravindra Pratap on 2013-08-01

Really awesome article sir... Thank u very much

Posted by Mahendra Singh on 2013-07-20

Sir, Please tell me difference between Abstraction and Encapsulation also give code of Encapsulation Implimentation.

Posted by Amruta on 2013-07-09

well explained..

Posted by Gopi reddy k on 2013-06-27

hi , i want to interview questions

Posted by Ramdas Ananddas Chavan on 2013-06-11

its very nice concept.and very help ful to me understnad