3

I hear that Abstraction is a technique that helps us identify which specific information should be visible, and which information should be hidden. Encapsulation is then the technique for packaging the information in such a way as to hide what should be hidden, and make visible what is intended to be visible.

I understand Encapsulation well... A simple example would be a Method that calculates calories based on the parameters we provide. CalculateCalories(x,y,z) Here I don't know how this method calculates calories but I can call it to calculate the calories.

Abstraction is what I am confused about. What are those techniques that Abstraction uses to help identify which specific information should be visible, and which information should be hidden? Or am I misunderstanding the concept?

Could anyone give an intuitive example?

1
9

In functional programming, there is a concept called Map. Map is a higher-order function that applies a function to each element in a list.

In ordinary code, you would do something like:

var list = new List<int> { 1,2,3,4,5,6 };

var newList = new List<int>();

foreach(var item in list)
{
    newList.Add(item * 2);
}

// newList --> { 2,4,6,8,10,12 }

Rather than writing a loop each time, you create a Map method:

public List<T> Map<T>(this List<T> list, Func<T,TResult> function)
{
    var newList = new List<T>()

    foreach(T item in enumeration)
    {
        newList.Add(function(item));
    }

    return newList;
}

Now, instead of writing a loop each time, you can simply write:

var list = new List<int>() { 1,2,3,4,5,6 };

var modifiedList = Map(list, x => x * 2); // { 2,4,6,8,10,12 }

and get the same result.

The Map method is an abstraction over writing a loop to modify each member of a list.


I haven't mentioned it yet, but the <T> syntax is also an abstraction. It's called generics; it means that you can write a function that can operate on different data types, instead of writing functions for each data type.

All abstraction is a form of generalization: writing code that applies to more than one specific situation.

4
  • Wouldn't Map method be encapsulation as well? It does hide the design from where it is called.
    – user793468
    Apr 29 '14 at 16:04
  • 1
    Encapsulation should be more properly viewed as a form of information hiding. Encapsulation is a programmer's way of hiding the implementation details of a class or method from a user. But classes and methods are also abstractions, so there is some conceptual overlap. Apr 29 '14 at 16:06
  • What does "Information" mean here? Are we hiding class members(methods/functions etc) or how class members(methods/functions etc) are written? Does it hide the design(class methods/properties) using Interfaces/Abstract classes from other classes which are related?
    – user793468
    Apr 29 '14 at 16:24
  • All of the above. But Encapsulation as a concept is primarily applied to the internal implementation of methods and objects. Apr 29 '14 at 16:27
7

I hear that Abstraction is a technique that helps us identify which specific information should be visible, and which information should be hidden.

Wrong. Thats still part of encapsulation.

Abstraction is when you have multiple interchangeable behaviors that are manipulated through common interface. The multiple thing is the most important thing. In OOP, abstractions are primarily achieved through inheritance with virtual methods or interfaces and their realization.

1
  • 2
    +1: I think that in the OP's question, the Abstract part would be an interface called ICalorieCalculator. Therefore any class that needed to calculate calories would want to be provided with an ICalorieCalculator, and the user of the class could provide it with any concrete implementation they want. Apr 3 '14 at 19:15
3

In layman's words:

Writing a solution with OOP is like creating a model of the problem, making the problem look easier, thus allowing to write good enough solutions.

But we don't need to model the whole thing down to the most minute details, because the model would be as complex as the problem itself. That would be like creating a map with a scale of 1:1. Such a map would be as big as the real thing.

We create a simplified model with the things that are crucial to the solution. Abstracting ourselves from the minutiae, not unlike a vehicle's suspension abstracts the driver from the unevenness of the road.

That's called generalization, or abstracion.

When you really need to make something very specific you do the opposite: you get the general solution and built on it. That's called specialization.

Don't write code that sorts an array of people's names.

Write code that sorts an array of strings. That is generic enough to sort people names, country names and animals scientific names.

Concrete-man (fictional character) would have written diffent sorting methods that do almost the same.

Abstraction usually takes place on a whiteboard, a napkin or the mind of the designer.

When you write the code the abstraction process already happened.

Abstraction is the process of deciding what classes have in common.

Most of the time it doesn't even have to do with code.

2

Well I will explain abstraction with a real world example. Say in your house you do have an electric plug and many devices can connect to the same plug but plug will never have an idea which device it is connected to, in other words the details of the devices is abstracted (hidden) to the plug.

Think what if we connect a device directly to electric wire without a plug? Say connect a bulb directly to a wire, then wire knows which device it is connected to and when ever we need to replace the bulb then we have to remove the wire connection from the bulb, which means bulb is tightly coupled with the wire. In other words bulb and wire knows the details where it is connected to, means not abstracted.

In object oriented world abstraction works exactly same. The class which consume other classes function/property doesn't need to know which classes function/property it is consuming and everything should be abstracted with an interface / abstract class.

Let me code the same example. Here I have a class "ElectricPlug", which is running a device. But the class "ElectricPlug" doesn't have any idea which device it is running. It can be any class implementing the interface "IDevice", which means the implementation of "RunDevice" is abstracted from "ElectricPlug". Here is the full sample code,`enter code here,

class Program
{
    static void Main(string[] args)
    {
        ElectricPlug electricPlug = new ElectricPlug(new Bulb());
    }
}

public class ElectricPlug
{
    private readonly IDevice _device;
    public ElectricPlug(IDevice device)
    {
        _device = device;
    }

    public void Run()
    {
        _device.Rundevice();
    }
}


public interface IDevice
{
    void Rundevice();
}


public class Bulb : IDevice
{
    public void Rundevice()
    {
       Console.WriteLine("Switched on bulb");
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.