Software Abstraction vs. Encapsulation
Tags: softwaredesign, architecture
Published: 2021-01-28 19:55:27

---
title: Software Abstraction vs. Encapsulation
published: true
date: 2020-10-27 21:34:29 UTC
tags: SoftwareDesign,SoftwareArchitecture
canonical_url: https://eamonkeane.dev/software-abstraction-vs-encapsulation/
---

 ![Software Abstraction vs. Encapsulation](https://images.unsplash.com/photo-1518780664697-55e3ad937233?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=2000&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)

I'm currently making my way through the highly commended book [Code Complete](https://www.amazon.com/gp/product/0735619670/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0735619670&linkCode=as2&tag=eamonkeane-20&linkId=d29c25cf9cb592f4da4634c1769b138e) by Steve McConnell.   
  
This book consistently ranks in the top 5 books for software developers. I've only just started to read it but it's plaudits are well deserved. There's so much value packed into it.

I'm going to distill what I believe the essential differences between Abstraction and Encapsulation.

## Abstraction

![Software Abstraction vs. Encapsulation](https://images.unsplash.com/photo-1524169358666-79f22534bc6e?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=2000&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)<figcaption>Photo by <a href="https://unsplash.com/@badashphotos?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Ash Edmonds</a> / <a href="https://unsplash.com/?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Unsplash</a></figcaption>

Steve describes abstraction as the ability to engage with a concept while safely ignoring _some_ of its details. He goes on to say that anytime we work with an aggregate of some sort we're working with an abstraction. We don't say that we're going to go visit someone's combination of glass, wood, concrete and nails, we say that we're going to visit someone's _house._ We've just performed an _abstraction_. We ignore the little things and lump them into the concept of a house.

Good classes allow us to concentrate on working with their interfaces (the pieces we "see")  and ignoring how they work under the hood. We might have a class named _SchoolManager_ and it has a method named _EnrollStudent_.

When we work with an instance of the _SchoolManager_ class and we want to enroll a student, we can concentrate on working with the _EnrollStudent_ method and trust that it does what its supposed to. We can ignore irrelevant details at this level.   
  
Under the hood, _EnrollStudent_ might add a student to a collection but when working with the _StudentManager_ object, we don't care about that.

Most real world objects are abstractions. Think of the objects around you, they're all abstractions.

Steve finishes off by saying that Abstraction allows you to take a simpler view of a complex concept.

## Encapsulation

![Software Abstraction vs. Encapsulation](https://images.unsplash.com/photo-1536160885591-301854e2ed04?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=2000&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)<figcaption>Photo by <a href="https://unsplash.com/@allthestars?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Sophie Dale</a> / <a href="https://unsplash.com/?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Unsplash</a></figcaption>

Where abstraction says '_You can look at an object at a high level of detail.'_ Encapsulation goes on to say that '_you aren't allowed to look at an object at ANY OTHER level of detail. '_

The author uses the analogy of a house and a door. Encapsulation is a way of saying that you can look at the outside of a house and you can see that there's a door. However, you can't get close enough to make out how the door is made or what material it's made off. Encapsulation prevents you from seeing the detail of the door. This is by design and for your own good!

<!--kg-card-begin: markdown-->

> Encapsulation helps to manage complexity by forbidding you to look at the complexity.

<!--kg-card-end: markdown-->

If this book sounds like it might be useful to here's an affiliate link you can use:  
[Code Complete by Steve McConnell](https://www.amazon.com/gp/product/0735619670/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0735619670&linkCode=as2&tag=eamonkeane-20&linkId=d29c25cf9cb592f4da4634c1769b138e).   
This helps with the cost of running my blog.