Abstraction concept in OOP

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

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

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

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.

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");
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Abstraction concept in OOP

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

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

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

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.

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");
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật