What Is Abstraction in C#

How abstraction and encapsulation differ?

Abstraction means to show only the necessary details to the client of the object

Actually that is encapsulation. also see the first part of the wikipedia article in order to not be confused by encapsulation and data hiding. http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)

keep in mind that by simply hiding all you class members 1:1 behind properties is not encapsulation at all. encapsulation is all about protecting invariants and hiding of implementation details.

here a good article about that.
http://blog.ploeh.dk/2012/11/27/Encapsulationofproperties/
also take a look at the articles linked in that article.

classes, properties and access modifiers are tools to provide encapsulation in c#.

you do encapsulation in order to reduce complexity.

Abstraction is "the process of identifying common patterns that have systematic variations; an abstraction represents the common pattern and provides a means for specifying which variation to use" (Richard Gabriel).

Yes, that is a good definition for abstraction.

They are different concepts.
Abstraction is the process of refining away all the unneeded/unimportant attributes of an object and keep only the characteristics best suitable for your domain.

Yes, they are different concepts. keep in mind that abstraction is actually the opposite of making an object suitable for YOUR domain ONLY. it is in order to make the object suitable for the domain in general!

if you have a actual problem and provide a specific solution, you can use abstraction to formalize a more generic solution that can also solve more problems that have the same common pattern. that way you can increase the re-usability for your components or use components made by other programmers that are made for the same domain, or even for different domains.

good examples are classes provided by the .net framework, for example list or collection. these are very abstract classes that you can use almost everywhere and in a lot of domains. Imagine if .net only implemented a EmployeeList class and a CompanyList that could only hold a list of employees and companies with specific properties. such classes would be useless in a lot of cases. and what a pain would it be if you had to re-implement the whole functionality for a CarList for example. So the "List" is ABSTRACTED away from Employee, Company and Car. The List by itself is an abstract concept that can be implemented by its own class.

Interfaces, abstract classes or inheritance and polymorphism are tools to provide abstraction in c#.

you do abstraction in order to provide reusability.

What is the difference between Abstraction and Inheritance?

First of you should be aware, that here is always some leeway in interpreting such terms and concepts. Below is my take on it.

Abstraction is the concept while inheritance is a technical realization.


Abstraction in general refers to the leaving out of (unnecessary) details. The opposite direction is concretization. Generalization is also often used in this context and basically means the same as abstraction.

In the context of computer science this can be used to describe several ideas:

  • One is the modelling of domain concepts. As in the class Car is an abstraction of real world automobiles. It uses an engine and four wheels to transport 1-5 people. Obviously this is nowhere near the informational density to describe a real car, but it may very well be all that is needed for the context of this application.

  • An other is to use it to describe the conceptual relation between multiple domain entities: A Car is a Motorvehicle. A Bus is also a Motorvehicle. Both are used to transport people. The Motorvehicle is the abstraction of both Car and Bus. It describes the idea of transporting people, while leaving out the detail of how many.

  • A third is the difference between an interface and an implementation. The interface abstracts the implementation by hiding the implementation details and only representing the surface area with which one may interact.


Inheritance is one method of realizing abstractions in code. It describes the process of taking a base class (this is the more general or abstract thing), inheriting all of its features/properties/behavior/meaning and adding some more details (or overriding some of the existing) to create the derived class (this is the more concrete thing).

What is an abstract class?

  1. most commonly to serve as a base-class or interface (some languages have a separate interface construct, some don't) - it doesn't know the implementation (that is to be provided by the subclasses / implementing classes)
  2. abstraction and re-use
  3. when the base-class can provide no meaningful default-implementation for a method (but allowing subclasses to re-use the non-abstract parts of the implementation; any fields, non-abstract methods, etc)

For example:

public abstract class Stream { /* lots of code, some abstract methods */ }

What the heck is a stream by itself? What kind of stream? a stream to a file? a network? a memory buffer? Each may have different and unrelated ways of reading / writing, but provide a common API. It makes no sense to create just a Stream, but via the abstract class, you can code to the Stream API without knowing the details:

Stream s = CreateStream(...); // I don't *care* what kind of stream
s.Write(new byte[] {1,2,3,4,5});
s.Close();

c# making object in different ways (abstraction used)

Starting from class definition. In my experience it is better to start with concrete implementation and then later (only if needed) extract some abstraction.
I'm not sure whose words are these but: "Abstraction should be discovered". So you shouldn't start your design from abstract keyword (in your example I would delete abstract class)

Another thing is that classes should encapsulate state and behavior and in your example you don't have a state (numbers you are passing into constructor are not store anywhere). It means that you would be fine only with static method that calculate

public static class Addison
{
public static object Calculate(object value1, object value2)
{
return (double)value1 + (double)value2;
}
}

and you can use it:

class Program    
{
static void Main()
{
object addison = Addison.Calculate(4, 6); //OBJ 1
object addison2 = Addison.Calculate(4, 6); // OBJ 2
Console.ReadKey();
}
}

If you would like to actually encapsulate state and behavior then

public class Addison
{
private object _value1;
private object _value2;
public Addision(object value1, object value2)
{
_value1 = value1;
_value2 = value2;
}
public object Calculate()
{
return (double)_value1 + (double)_value2;
}
}

class Program
{
static void Main()
{
Addison addison = new Addison(4, 6); //OBJ 1
Addison addison2 = new Addison(4, 6); // OBJ 2
Console.WriteLine(addison.Calculate());
Console.WriteLine(addison2.Calculate());
Console.ReadKey();
}
}

In above example you values 4 and 6 are stored in private (not accessible from outside) fields. And Calculate method use them to produce result.

Now if you ask what is a difference between addison and addison2:
- this are two different instances of the same class
- they occupy two different places in memory (their references are not equal)



Related Topics



Leave a reply



Submit