Abstraction in Object-Oriented Programming: Benefits and Examples

Hatef Palizgar
6 min readDec 18, 2022

Introduction

Object-oriented programming (OOP) is a popular programming paradigm that allows developers to write organized and efficient code. Abstraction is an important concept when it comes to OOP, as it allows for the simplification of complex coding tasks. In this post, we will explore the concept of abstraction in OOP, how it helps us, and the benefits of using it in our code. We will also discuss what can happen if we don’t use abstraction when coding.

What is an abstraction in OOP?

Abstraction is a powerful concept in OOP that allows developers to hide details and complexity in their code. This means that when writing code, we don’t need to think about small details such as the implementation of the code. Instead, we can focus on the bigger picture of the programme and think about the desired behaviour. In OOP, abstraction is typically achieved through abstract classes and interfaces.

Ok, but can you explain abstraction to me in simple terms?

Absolutely. I can start with a simple mini-story to make more sense to you. Below is that mini-story called “The Different Kind of Love”:

Photo by Shaira Dela Peña on Unsplash

Hatef loved his girlfriend and his dog, each in their own unique ways. When it came to his girlfriend, he gave her lots of presents and flowers, trying his absolute best to express his undying love for her. His days and nights were filled with thoughts of her, and he tried his best to make her happy.
Hatef’s love for his dog showed a different side to his affection. He often took his dog outside to play with the other animals, and he made sure to bathe and groom him regularly. He could be seen throwing the ball around in the park or running around, trying to get the dog to chase him.
But when it came to deeper things, Hatef knew his love for his girlfriend was special, and it was something his dog didn’t quite understand. He often found himself having conversations with his girlfriend late into the night, talking about their hopes and dreams and future plans.
Hatef was never afraid to express his feelings to his girlfriend, and he was never afraid to admit when he was wrong. He knew he was well taken care of by his girlfriend and that she was the one constant in his life that would never go away.
However, the same couldn’t be said about his dog. Even though Hatef loved him and spent time with him every day, he knew that one day his dog would pass away, and he would have to say goodbye. This was something that was hard for Hatef to come to terms with, and it was something he often struggled with.

Love can be expressed in many different ways. In the mini-story, Hatef expressed his love for his girlfriend in a different way than he did for his dog. For his girlfriend, Hatef gave presents and flowers, and he spent time talking with her about their hopes and dreams. For his dog, Hatef took him outside to play, bathed and groomed him, and ran around trying to get him to chase him.

Both his girlfriend and his dog were important to him, but the way he showed his love for each was different. He knew that his relationship with his girlfriend was something special and would last, while his relationship with his dog was more fleeting and he would one day have to say goodbye.

Now, let’s turn this story into a simple piece of Java code:

Girlfriend and Dog

Below is the code for the classes Girlfriend and Dog .

Note: I’ve written code logic as comments to avoid messing with the example. Take it as a pseudocode.

public class Girlfriend {

public void loveMe() {
// Give presents and flowers
// Spend time talking about hopes and dreams
}
}


public class Dog {

public void loveWoofWoof() {
// Take dog outside to play
// Bathe and groom the dog
// Run around trying to get the dog to chase
}
}

Each class has a love method (loveMe and loveWoofWoof) defined, which corresponds to the information in the short narrative. TheGirlfriend class implements expressions of love with presents and flowers and spends time talking about hopes and dreams. TheDog class implements expressions of love by taking the dog outside to play, bathing and grooming the dog, and running around trying to get the dog to chase.

Now, let’s say Hatef is a Java class that has two methods, each for showing love to both his girlfriend and his dog:

public class Hatef {

public void showLoveToGirlFriend(GirlFriend gf){
gf.loveMe();
}

public void showLoveToDog(Dog dog){
dog.loveWoofWoof();
}

}

So far, so good. Look back at Hatef class. Can you spot a problem? The horrible fact about the above code is that for everything that Hatef loves in his life, we need to create a separate method. A method similar to showLoveToDog() that does nothing but call the love method of another object. This is called duplication in our logic. Additionally, ourHatef class should be aware of which methods of GirlFriend or Dog object to call. In other terms, Hatef needs to be concerned about how the love feature of each object is implemented (i.e. whether to call loveMe() or loveWoofWoof()).

Let’s fix this with the help of abstraction.

  1. We all agree that the only thing his girlfriend and dog have in common is the need for being loved. To make Hatef life easier so that he no longer cares about HOW he should show love to each, we create an interface called Loveble below:
public interface Lovable {
void love();
}

2. We change GirlFriend and Dog class to implement Lovable because both agree on the common need for being loved (remember interface is a form of contract):

public class Girlfriend implements Lovable {

@Override
public void love() {
// Give presents and flowers
// Spend time talking about hopes and dreams
}
}


public class Dog implements Lovable {

@Override
public void love() {
// Take dog outside to play
// Bathe and groom the dog
// Run around trying to get the dog to chase
}
}

3. Now we can change Hatef to love any object that is also lovable:

public class Hatef {

public void showLove(Lovable lovely){
lovely.love();
}
}

Yeeeeesss!!! We achieved it!

Hatefno longer needs to worry about the number of different lovely things he loves (no more duplication). He has a single method that can take any lovable object and show love to them without being concerned about their love method implementation. He ONLY needs to call their love() method.

Benefits of Abstraction

The benefits of abstraction in OOP are numerous. Firstly, it helps us write organised code that is easier to read and understand. This also means that our code is more efficient and less prone to errors. Additionally, abstraction helps us save time since we don’t have to worry about low-level details and can instead focus on the bigger picture. Finally, abstraction also makes our code more reusable, meaning that it can be easily adapted for other applications.

What if We Don’t Use Abstraction?

If we don’t use abstraction when coding, our code can become extremely inefficient and difficult to read. This is because there is no way to hide the details and complexity of the code, resulting in a large amount of code that is difficult to understand and debug. Additionally, without abstraction, our code may not be reusable and will be difficult to adapt to new applications.

Conclusion

In conclusion, abstraction is an important concept in object-oriented programming that allows us to write organized and efficient code. Abstraction helps us save time and makes our code more reusable, which is essential when writing code. However, if we don’t use abstraction when coding, our code can become difficult to read and inefficient. Through the code examples in this post, we can see how abstraction is used to hide the details and complexity of a programme, allowing us to focus on the desired behaviour.

Before you go, here’s an inelegant poem on abstraction:

Coding with abstraction, creating a great life,
Java is the language that is so full of strife,
For the complex tasks and complex minds,
Abstraction makes life of all kinds.

Strings and loops and numbers, too,
Objects made with abstraction through and through,
Classes floating on the wings of the night,
Producing the power to do what is right.

Please don’t forget to follow me if you enjoy my approach to translating difficult terms into easier language.

--

--