Abstraction is a model of a future data type, but without any specific implementation of its functionality in the form of algorithms. Defining data types through abstraction provides flexibility and extensibility of your code.
Abstraction lists all the main characteristics of the future types. But each data type, which implements the abstraction, will need to define its own logic in each of its methods.
In other words, Abstraction is a contract, signed by data types, if they want to implement it.
Let’s assume that our application performs logging of some calculation process.
In some cases, we need to perform logging to the SQL database, and sometimes – just to an XML file, or even just to a text file. Logging can be made in many different ways, and trying to predict all the options for every occasion, defining them directly within the main logic class through overloaded methods – is a bad idea.
It makes more sense to define a separate data type, which will be responsible just for the functionality of logging. And each time we need a different algorithm of logging we create a class that implements the abstraction and provides the required logic in its methods. Thus all these classes will implement the same base interface – ILog
void LogData (double num);
// implementation of abstraction
class DBLogger: ILog
public void LogData (double num)
// Logging to database
// class that performs the calculations
public Calculator (ILog log)
// set a particular type of logging
this.log = log;
public void Total (params double  arr)
double result = 0;
// do logging
In this example, the interface ILog is an abstraction. Class BDLogger – is its implementation, which defines how the record is written into the Sql database.
The Calculator class constructor allows to determine which type of logger will be used in this instance of the class. Due to the fact that the constructor of class Calculator takes a reference to ILog interface (abstraction) as a parameter, we can pass an object of any class that implements this abstraction.
Now let’s see how it works:
static void Main ()
DBLogger logger = new DBLogger ();
Calculator calc = new Calculator (logger);
If we need a different way of logging in future, for example, logging to an XML file, we shall write a new class implementing ILog interface. In its method LogData() we shall encapsulate algorithm of writing log information to a file of XML format. We use the same option for the other similar cases.
Thus we can say that constructing a new class according to a predetermined pattern of functionality is the main idea of Abstraction.