abstraction

What Is Abstraction?

We’re continuing on our journey learning about the four OO programming concepts of Abstraction, Encapsulation, Polymorphism and Inheritance. Today we’ll take a closer look at Abstraction.

OK, let’s get into it.

Abstraction is closely related to Encapsulation. Encapsulation is about hiding information. It’s the idea that callers of a public method on a class should not have access to the internals of that class. I.e. how it carries out its work. 

Abstraction is the other side of Encapsulation. Abstraction represents the external projection of the encapsulated, hidden information. In other words, if Encapsulation is about the ‘HOW the work is done‘, then Abstraction is about ‘WHAT work is done‘.  In more concrete terms, Abstraction is the method signature, while Encapsulation represents the method body.

For example, we want to register a new customer in our system. We have written a class for this high-level business logic, RegisterNewCustomerUseCase. RegisterNewCustomerUseCase has a Register() method that does the work of registering a new customer. In C#, the signature of Register could be 

public async Task<Customer> Register(CustomerRegistration registration)

That is the Abstraction.

On the other hand, Encapsulation represents the method body, the detail:

  {
     await Validate(registration);
     var customer = registration.ToCustomer();
     await Repository.SaveCustomer(customer);
     return customer;
  }

Encapsulation and Abstraction are inextricably linked. It doesn’t make sense to have one without the other.

 

Suggested Reading: 

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply