Thinking about code — what is a level of abstraction anyway?

Kamal Joshi
4 min readApr 9, 2018

--

Programming languages allow you to express code at various levels of abstraction. What is a level of abstraction anyway?

This post is intended for the learners of programming and especially those who are wondering why should they ever learn about abstract classes and interfaces available in object oriented programming languages.

To help understand the concept of a level of abstraction, consider this situation.

You visit a restaurant and place an order. How do you communicate with a waiter? You might say, I want a certain dish and a certain drink. You normally don’t tell the waiter how to make the dish or what ingredients to use. You just pass a message and expect that to be handled. This communication can be said to happen at a high level of abstraction. It has just enough details for the waiter to understand what you want done.

The waiter goes back to the kitchen and passes this information to a chef who is in charge of preparing the dish. Let us assume the chef has a few helpers. The chef gives instructions to the helpers that may proceed as follows - chop vegetables, knead dough, bake at 250F for 15 minutes, and so on. This is another level of abstraction — one that is suitable for communication between the chef and the helpers. Again, with just enough details for the helpers to understand what the chef wants done. From your point of view, however, this interaction happens at a lower level of abstraction.

We may go further deep into this process and imagine that each helper now performs tasks such as using a knife to chop the vegetables, placing a dish into the oven, turning on the oven, and setting a timer. This covers just the specific and sufficient details for them to finish their respective tasks. From your point of view, this interaction happens at an even lower level of abstraction.

The helpers in turn communicate with their brains and the brains with their bodies at an even lower level of abstraction. The brains makes it appear automatic. We don’t know and don’t need to know how the brains passed what messages to different parts of their bodies to carry out those tasks. The level of abstraction is now too low for us to understand or care about.

You probably only cared about the highest level of abstraction in this entire interaction (The one where you place an order) and yet you get your job done (the meal is served). At each layer of interaction, the workers in charge simply understood a high level message and broke it down into details that were relevant to them and you got your meal in the end without having knowledge of any lower level details.

Object oriented programming languages such as Java allow you to write your software in a manner that resembles this structure of interaction. An object typically invokes a method on an interface which describes the high level message being passed. In this case, the customer is (you are) invoking an orderMeal method on any person that can handle the Waiter interface (Waiter W for example). You also passed the relevant information such as the dish name and the drink name. These are method parameters. The waiter in turn takes the order, does some housekeeping such as recording that information in an order management system and then invokes a method such as prepareMeal on a person that can handle the Chef interface (Chef C for an example). The chef in turn passes messages by invoking methods such as chopVegetable on a person handling (implementing) the Helper interface (helper H) and so on. You get the idea.

In terms of software and computer systems, each client object ( that wants something done) invokes a method on an object that can perform the job. At this point, the level of abstraction changes, and the task becomes a bit more detailed. This task is further broken down to even lower level constructs . This process continues until a very low level abstraction such as a CPU instruction gets around to executing it.

Interactions this way happen around us all the time, not just in computer software. This is so natural that we don’t even think about them as they happen. However, we need to think this way when we seek to write efficient, extensible and changeable object oriented code that represents such interactions.

It is because of these levels of abstraction, that so many processes can run efficiently. Businesses can focus on their core competency and delegate other tasks to other businesses (who could be experts in handling those other tasks). This also represents the flexibility of the system you can build, with these ideas. For example, a restaurant can work with a cleaning crew from company A this year and change it with company B the next year without impacting the rest of their business. In object oriented terms, you could change an implementation with another one as long as they implement the same interface.

There is a well known object oriented design principle called SLAP ( Single Level of Abstraction Principle) which says you must keep instructions in a method at a single level of abstraction. I hope you understand now what this principle means. I hope this helps you consider and implement suitable levels of abstraction in the design and development of your code.

--

--