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
4. Rear View Mirror
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
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(); } }
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
By Mahesh Shisode on 2018-11-03Hi 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
By Afroj on 2016-05-28want to know more about abstract class and abstract function...
By Bhagwan Binay Thakur on 2016-04-23Sir I want to know all access specifiers implementation is an example of Encapsulation or abastarction. I am very confused.
By Vignesh on 2016-04-22Nice
By Nilesh Yewale on 2016-04-10dear sir plz send me complete example of abstract , interface , encapsulation class
By Venkatesh mahendru on 2016-03-02So, what is encapsulation then if we can hide things in abstraction.
By Vijay Kumar Prajapati on 2016-02-08Excellent Sir......Thanks a lots!
By Shobana on 2015-11-28why we are using.. private access specifier in our program.. means Unnecessary things in our code??????
By Velavan on 2015-11-23very good explanation friend,i am really got an whole concept and understand all the things about the abstraction
By Lingaraj behera on 2015-10-16i want know about oops concepts.
By Venkatprasad on 2015-08-06what is the difference between encapsulation and abstraction
By Ram on 2015-04-15Very good real time example
By Manojbritto on 2015-01-31nice explanation thank u very much
By Amol on 2015-01-20nice blog
By Yogesh on 2014-10-21Gurunatha Dogi Yr Your Blog Is Realy Help Full For Me To Understand The Concept of Oops Again Thanks..
By Ram Gosavi on 2014-09-29Thanks 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-23Its very good explained oop concepts very help full , thanks
By Vijay Gujar on 2014-09-21very nice article sir....thanks for that..
By Sakshi on 2014-09-07explain the exact diff between abstraction and encapsulation.
By Vinoth on 2014-09-02excellent
By Anup Dey on 2014-08-06Thanks a lot !!
By Hari krishna on 2014-07-15Excellent Code.. Easy Way To Understand
By Rajendra on 2014-07-10Thanks a lot....!!! Really nice explanation..now onwards threre wil be no confusion regarding abstraction and encapsulation .really nice thanks once again
By Bhanu Pratap singh on 2014-07-08Nice example thanks bro
By Hendrawan Haryanto on 2014-05-23Very clear explanation.. Wow thank u so much..
By Srikanth on 2014-05-23Very nice explanation, thank u
By Devineni on 2014-05-17Very nice explanation
By Sushant on 2014-04-23thank u....
By Mohd Ilyas on 2014-04-23Excellent explanation,I was looking for similar explanation to implement to write into my blog. keep it up nice..
By RAJKUMAR M on 2014-04-07I was search about abstraction but i can't get correct explanation but in this blog its very very nice to understand.. Thanks a lot
By Mahima on 2014-03-27very nice explaination...thanks
By Joy on 2013-11-29I was searching some real time good example. This article really helped me lot. Thanks.
By Jacob on 2013-11-02Nice 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-29Thank you very much for providing this Article please let me know ,if you update new on your blog
By Srinivas D V on 2013-10-22Nice Example... Good work
By Ravindra on 2013-10-17You 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-16thank u....
By Nitin Suri on 2013-10-01very briefly and nicely explained
By Venkatreddy on 2013-08-28very nice explanation..
By Neha on 2013-08-17about abstractions must read it
By JUTURU DINESH on 2013-08-12thank q , i understand it easily ..thanks a lot
By Akil on 2013-08-06Then how we will implement the Encapsulation. Please waiting for your response.
By Suman on 2013-08-02Thank 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-01Really awesome article sir... Thank u very much
By Mahendra Singh on 2013-07-20Sir, Please tell me difference between Abstraction and Encapsulation also give code of Encapsulation Implimentation.
By Amruta on 2013-07-09well explained..
By Gopi reddy k on 2013-06-27hi , i want to interview questions
By Ramdas Ananddas Chavan on 2013-06-11its very nice concept.and very help ful to me understnad
Add a Comment
Thank you, for commenting !!
Your comment is under moderation...
Keep reading other blogs