locked
What is abstraction in C# ?

    Question

  • can any expert describes what is abstraction in C# ? I was searching in google, but can't find direct answer related to C#.

    so,I need an example with code.  Please explain with example ?

    Thanks
    Thank You
    Sunday, July 12, 2009 11:58 PM

Answers

  • What exactly do you need to know?

    An abstract class in C# means that the class *has* to be sub-classed. It can not be instantiated itself. Likewise an abstract method needs to have code put into it in the sub-class.

    A good use for an abstract class is if you want to implement the majority of the functionality that a class will need, but there are reasons that the class shouldn't be used directly.

    As an example, I have a ComboBox class. It basically handles everything necessary to be able to fill itself from a Codes DataSet, but it will not ever be able to, at design-time, know where that Codes DataSet comes from. Consequently, it is something that *must* be determined by a sub-class, which will know at design-time which Codes DataSet it will use (and the developer will then create a sub-class for every Codes DataSet they need a ComboBox for).

    So, to make it so that a developer will not attempt to use that ComboBox class directly, I have made it abstract and so it must be sub-classed. Make sense?
    ~~Bonnie Berent [C# MVP]
    • Marked as answer by Bin-ze Zhao Monday, July 20, 2009 3:14 AM
    Monday, July 13, 2009 1:26 AM
    Moderator
  • http://en.wikipedia.org/wiki/Abstraction_(computer_science)  ABSTRACTION

    The Wikipedia definition is pretty much on point.  But, understanding or explaining it is like explaing what the color blue is?  It is something that you must experience to understand.

    http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/a67ee0db-0dad-4715-9061-8864ed75ce6d  INTERFACES

    Understanding what abstracting your code means, and even writing abstract code yourself is one the major hurdles to "wrapping your head" around Object Oriented Programming.  The essence of abstraction is to reduce the dependencies between classes.  This is known as making your classes "loosely coupled".  So that changes in one class do not ripple through to other classes, which forces you to go through the time-consuming process of tracking down all references, or usage, to the class to effect the changes.  Good luck hunting them all down because all to often the code will compile no problem.

    "Favoring composition over inheritance."  That is one of the pillars of OOP, and it speaks directly about abstraction.  Programming to the implementation of the code, or an interface.  Not programming to class inheritance, which can ripple throughout your application and make corrections a real nightmare.

    This just scratches the surface.  The link provides several examples of interface implementations.  Each of which has a different intent, or purpose to how the interfaces might be used.

    Rudedog  =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Bin-ze Zhao Monday, July 20, 2009 3:14 AM
    Monday, July 13, 2009 3:13 AM
    Moderator
  • Encapsulating is keeping all of the information about something together.

    So building on my prior Customer example, I abstracted out the appropriate properties and methods for a customer when building a car sales application. When I build the Customer class, I keep all of the customer properties and methods in the same class, thereby encapsulting all of the Customer functionality.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Bin-ze Zhao Monday, July 20, 2009 3:14 AM
    Saturday, July 18, 2009 12:16 AM
  • hiding the complexity is what we call as abstraction. here we provide some set of interfaces to provide the functionalities with out disclosing the complexity.
    Friday, April 29, 2011 2:18 PM

All replies

  • What exactly do you need to know?

    An abstract class in C# means that the class *has* to be sub-classed. It can not be instantiated itself. Likewise an abstract method needs to have code put into it in the sub-class.

    A good use for an abstract class is if you want to implement the majority of the functionality that a class will need, but there are reasons that the class shouldn't be used directly.

    As an example, I have a ComboBox class. It basically handles everything necessary to be able to fill itself from a Codes DataSet, but it will not ever be able to, at design-time, know where that Codes DataSet comes from. Consequently, it is something that *must* be determined by a sub-class, which will know at design-time which Codes DataSet it will use (and the developer will then create a sub-class for every Codes DataSet they need a ComboBox for).

    So, to make it so that a developer will not attempt to use that ComboBox class directly, I have made it abstract and so it must be sub-classed. Make sense?
    ~~Bonnie Berent [C# MVP]
    • Marked as answer by Bin-ze Zhao Monday, July 20, 2009 3:14 AM
    Monday, July 13, 2009 1:26 AM
    Moderator
  • http://en.wikipedia.org/wiki/Abstraction_(computer_science)  ABSTRACTION

    The Wikipedia definition is pretty much on point.  But, understanding or explaining it is like explaing what the color blue is?  It is something that you must experience to understand.

    http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/a67ee0db-0dad-4715-9061-8864ed75ce6d  INTERFACES

    Understanding what abstracting your code means, and even writing abstract code yourself is one the major hurdles to "wrapping your head" around Object Oriented Programming.  The essence of abstraction is to reduce the dependencies between classes.  This is known as making your classes "loosely coupled".  So that changes in one class do not ripple through to other classes, which forces you to go through the time-consuming process of tracking down all references, or usage, to the class to effect the changes.  Good luck hunting them all down because all to often the code will compile no problem.

    "Favoring composition over inheritance."  That is one of the pillars of OOP, and it speaks directly about abstraction.  Programming to the implementation of the code, or an interface.  Not programming to class inheritance, which can ripple throughout your application and make corrections a real nightmare.

    This just scratches the surface.  The link provides several examples of interface implementations.  Each of which has a different intent, or purpose to how the interfaces might be used.

    Rudedog  =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Bin-ze Zhao Monday, July 20, 2009 3:14 AM
    Monday, July 13, 2009 3:13 AM
    Moderator
  • hi BonnieB,

                     my question is about abstraction ? There is a concept in oops called abstraction. can u describe more about that?

    Thanks
                     

    Thank You
    Monday, July 13, 2009 7:19 AM
  • Absratration means just like wrapping up data into a single unit so i can't accessible out side the world.

    the realtime eg. is A.C(AIRCONDITIONER) in this lot of thing are wrappend that are invisible like fan,electric-motor,etc etc
     this is just simple eg.
    Sharad Sharma Pursuit Technologies Bangalore
    Monday, July 13, 2009 7:39 AM
  • Rudy had a good post.
    ~~Bonnie Berent [C# MVP]
    Tuesday, July 14, 2009 3:20 AM
    Moderator
  • hi Sharma,

                     Then what is the difference between abstraction and Encapsulation?
              
    Thank You
    • Proposed as answer by Pratiyush92 Sunday, January 13, 2013 5:08 PM
    • Unproposed as answer by Pratiyush92 Sunday, January 13, 2013 5:08 PM
    Friday, July 17, 2009 11:23 PM
  • Abstraction is thinking about something a certain way.

    I can look at a tree and think about shade, abstracting out only the parts of a tree that provide shade.
    A child can look at a tree and think about a climbing structure, abstracting out only the those parts that make climbing fun.

    So take Customer.

    If I am building an Invoicing system, I abstract out only the features of a customer that affect invoicing, such as customer name, address, preferred sending method, and so on.

    If I am building a car sales system, I abstract out only the features of a customer that affect car sales, such as customer name, phone, credit rating, and so on.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Saturday, July 18, 2009 12:14 AM
  • Encapsulating is keeping all of the information about something together.

    So building on my prior Customer example, I abstracted out the appropriate properties and methods for a customer when building a car sales application. When I build the Customer class, I keep all of the customer properties and methods in the same class, thereby encapsulting all of the Customer functionality.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Bin-ze Zhao Monday, July 20, 2009 3:14 AM
    Saturday, July 18, 2009 12:16 AM
  • hiding the complexity is what we call as abstraction. here we provide some set of interfaces to provide the functionalities with out disclosing the complexity.
    Friday, April 29, 2011 2:18 PM
  • http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b54c98bf-e208-4332-b8f3-1dae944676ae/

     

    I have a post in that thread giving an example of abstraction and encapsulation. The thread is relevant to a good extent and many experts have contributed to it. Please go through them to get additional information if you want :)

    Friday, April 29, 2011 5:53 PM
  • Two concepts that go together in the object oriented approach are Encapsulation & Abstraction. Abstraction is the representation of only the essential features of an object, while Encapsulation is the hiding of the non-essential features.
    Think of a person driving a car. He does not need to know the internal working of the engine or the way gear changes work, to be able to drive the car (Encapsulation). Instead, he needs to know things such as how much turning the steering wheel needs, etc (Abstraction).
    Consider the example from the programmer’s perspective who wants to allow the user to add items to a list. The user only needs to click a button to add an item (Abstraction). The mechanism of how the item is added to the list is not essential for him (Encapsulation).
    Why do we need Encapsulation & Abstraction?
    Programming is all about concepts, different from coding where we basically translate the logic to language specific codes. When given a problem statement, there might be a number of ways to solve it. The logic devised to solve it will depend of the programmer. What we must learn to implement is the most efficient approach; efficient in terms of time, memory & cost. Object oriented methodologies such as these are what allow us to maintain efficiency. 
    By using encapsulation & abstraction, we reduce complexity by ignoring unimportant details and we maintain effectiveness by representing important ones.
    Monday, December 12, 2011 7:09 AM
  • Abstraction is just hiding up the complexity.like we made an object of any class then from that objects we can use the all property and member of that class .so here is hiding up the complexity of the class.

    we don't know their what is content of class we are just using that functions without knowing that so this is abstraction(means hiding up ).  

    Marked as answer by chhabra Ankur  on March 14,2012 

    Wednesday, March 14, 2012 2:58 PM
  • The thread is close to three years old and has already been answered...
    Wednesday, March 14, 2012 3:15 PM
  • really very good description of abstraction , to hide the complexity we use it...

    Sunday, February 02, 2014 8:57 AM
  • When you interact with objects in the world, you are often only concerned with a subset of their properties.

    Sunday, March 16, 2014 5:58 PM
  • I'm locking this thread, people shouldn't post in such an old thread.

    Regards, Eyal Shilony

    Sunday, March 16, 2014 9:11 PM
    Moderator