Abstraction in Coding Explained (OOP)

_config.yml

Today we will be discussing a word that gets thrown around a lot in OOP (Objected Oriented Programming) circles a lot. That word is “abstraction”. It takes me back to my law school days. It seems that programmers and lawyers (or at least law students and academics) have a lot in common: the jurist seeks to reduce various disparate cases down to a single legal principle where as the programmer also seeks to draw similarities and commonalities between concepts he deems similar.

That sounds a bit abstract (please forgive the pun), so what does it mean? This is best illustrated by example.

Consider the following items, and let us draw out an abstraction (in terms of what they are) from them. Let’s say I take a stroll outside and I see the following parked on the street:

  • Toyota
  • Ford
  • Holden
  • Mitsubishi
  • Mazda

What is the common abstraction between them all? Well, one answer could be that:

  • they are all cars. And yes, that’s true. They are all cars. Another could be:
  • they are all vehicles. (This seems to be one level of abstraction higher than ‘car’)
  • they are all things. (And this seems one abstraction level higher, yet again).

Let’s use another example:

What is the common abstraction between the following:

  • Chimpanzee
  • Baboon
  • Bonobo
  • Gibbon
  • Gorilla
  • Orangutan

Well, I suppose they are all:

  • Primates or,
  • Monkeys, or
  • Mammals
  • Hominidae

Again, those are different levels of abstraction.

So when you writing code you will want to group together similarities. You can group them together under the rubric of a common abstraction. And you can then use some of form of polymorphism to take care of the details.

Consider this:

This brings about a discussion about classes and data structures in general, and how we should organise them. When you think about it, what are we doing with programs, and programming?

Well, generally speaking we have:

  • Data and we need to:

  • Manipulate that data, generally speaking, in order to get an output.

Right now the paradigm is like this:

  • We can store the data separately. In its own data structure. And we can expose that data to the world.

  • Or the other alternative is to hide the data, but to expose an abstraction of the data through an object. We don’t want to expose the actual data itself because then that would lead us to violations of the Law of Demeter.

More on this topic in upcoming posts.

Written on January 19, 2018