Abstraction in C# using an example

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

 

Author: Gurunatha Dogi

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

64x64
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 Shisode
64x64
By Afroj on 2016-05-28
want to know more about abstract class and abstract function...
64x64
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.
64x64
By Vignesh on 2016-04-22
Nice
64x64
By Nilesh Yewale on 2016-04-10
dear sir plz send me complete example of abstract , interface , encapsulation class
64x64
By Venkatesh mahendru on 2016-03-02
So, what is encapsulation then if we can hide things in abstraction.
64x64
By Vijay Kumar Prajapati on 2016-02-08
Excellent Sir......Thanks a lots!
64x64
By Shobana on 2015-11-28
why we are using.. private access specifier in our program.. means Unnecessary things in our code??????
64x64
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
64x64
By Lingaraj behera on 2015-10-16
i want know about oops concepts.
64x64
By Venkatprasad on 2015-08-06
what is the difference between encapsulation and abstraction
64x64
By Ram on 2015-04-15
Very good real time example
64x64
By Manojbritto on 2015-01-31
nice explanation thank u very much
64x64
By Amol on 2015-01-20
nice blog
64x64
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..
64x64
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.
64x64
By Rizwan Shahzad on 2014-09-23
Its very good explained oop concepts very help full , thanks
64x64
By Vijay Gujar on 2014-09-21
very nice article sir....thanks for that..
64x64
By Sakshi on 2014-09-07
explain the exact diff between abstraction and encapsulation.
64x64
By Vinoth on 2014-09-02
excellent
64x64
By Anup Dey on 2014-08-06
Thanks a lot !!
64x64
By Hari krishna on 2014-07-15
Excellent Code.. Easy Way To Understand
64x64
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
64x64
By Bhanu Pratap singh on 2014-07-08
Nice example thanks bro
64x64
By Srikanth on 2014-05-23
Very nice explanation, thank u
64x64
By Hendrawan Haryanto on 2014-05-23
Very clear explanation.. Wow thank u so much..
64x64
By Devineni on 2014-05-17
Very nice explanation
64x64
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..
64x64
By Sushant on 2014-04-23
thank u....
64x64
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
64x64
By Mahima on 2014-03-27
very nice explaination...thanks
64x64
By Joy on 2013-11-29
I was searching some real time good example. This article really helped me lot. Thanks.
64x64
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.
64x64
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
64x64
By Srinivas D V on 2013-10-22
Nice Example... Good work
64x64
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.
64x64
By NAMOHAR M on 2013-10-16
thank u....
64x64
By Nitin Suri on 2013-10-01
very briefly and nicely explained
64x64
By Venkatreddy on 2013-08-28
very nice explanation..
64x64
By Neha on 2013-08-17
about abstractions must read it
64x64
By JUTURU DINESH on 2013-08-12
thank q , i understand it easily ..thanks a lot
64x64
By Akil on 2013-08-06
Then how we will implement the Encapsulation. Please waiting for your response.
64x64
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?
64x64
By Ravindra Pratap on 2013-08-01
Really awesome article sir... Thank u very much
64x64
By Mahendra Singh on 2013-07-20
Sir, Please tell me difference between Abstraction and Encapsulation also give code of Encapsulation Implimentation.
64x64
By Amruta on 2013-07-09
well explained..
64x64
By Gopi reddy k on 2013-06-27
hi , i want to interview questions
64x64
By Ramdas Ananddas Chavan on 2013-06-11
its very nice concept.and very help ful to me understnad

Add a Comment