OOP Concept for Beginners: What is Abstraction? - Stackify (2024)

By: Thorben

| February 28, 2024

OOP Concept for Beginners: What is Abstraction? - Stackify (1)

Abstraction is one of the key concepts of object-oriented programming (OOP) languages. Its main goal is to handle complexity by hiding unnecessary details from the user. That enables the user to implement more complex logic on top of the provided abstraction without understanding or even thinking about all the hidden complexity.

That’s a very generic concept that’s not limited to object-oriented programming. You can find it everywhere in the real world.

Abstraction in the real world

I’m a coffee addict. So, when I wake up in the morning, I go into my kitchen, switch on the coffee machine and make coffee. Sounds familiar?

Making coffee with a coffee machine is a good example of abstraction.

You need to know how to use your coffee machine to make coffee. You need to provide water and coffee beans, switch it on and select the kind of coffee you want to get.

The thing you don’t need to know is how the coffee machine is working internally to brew a fresh cup of delicious coffee. You don’t need to know the ideal temperature of the water or the amount of ground coffee you need to use.

Someone else worried about that and created a coffee machine that now acts as an abstraction and hides all these details. You just interact with a simple interface that doesn’t require any knowledge about the internal implementation.

You can use the same concept in object-oriented programming languages like Java.

OOP Concept for Beginners: What is Abstraction? - Stackify (2)

Abstraction in OOP

Objects in an OOP language provide an abstraction that hides the internal implementation details. Similar to the coffee machine in your kitchen, you just need to know which methods of the object are available to call and which input parameters are needed to trigger a specific operation. But you don’t need to understand how this method is implemented and which kinds of actions it has to perform to create the expected result.

Different Types of Abstraction

There are primarily two types of abstraction implemented in OOPs. One is data abstraction which pertains to abstracting data entities. The second one is process abstraction which hides the underlying implementation of a process. Let’s take a quick peek into both of these.

Data Abstraction

Data abstraction is the simplest form of abstraction. When working with OOPS, you primarily work on manipulating and dealing with complex objects. This object represents some data but the underlying characteristics or structure of that data is actually hidden from you. Let’s go back to our example of making coffee.

Let’s say that I need a special hazelnut coffee this time. Luckily, there’s a new type of coffee powder or processed coffee beans that already have hazelnut in it. So I can directly add the hazelnut coffee beans and the coffee machine treats it as just any other regular coffee bean. In this case, the hazelnut coffee bean itself is an abstraction of the original data, the raw coffee beans. I can use the hazelnut coffee beans directly without worrying about how the original coffee beans were made to add the hazelnut flavour to it.

Therefore, data abstraction refers to hiding the original data entity via a data structure that can internally work through the hidden data entities. As programmers, we don’t need to know what the underlying entity is, how it looks etc.

Process Abstraction

Where data abstraction works with data, process abstraction does the same job but with processes. In process abstraction, the underlying implementation details of a process are hidden. We work with abstracted processes that under the hood use hidden processes to execute an action.

Circling back to our coffee example, let’s say our coffee machine has a function to internally clean the entire empty machine for us. This is a process that we may want to do every once a week or two so that our coffee machine stays clean. We press a button on the machine which sends it a command to internally clean it. Under the hood, there is a lot that will happen now. The coffee machine will need to clean the piston, the outlets or nozzles from which it pours the coffee, and the container for the beans, and then finally rinse out the water and dry out the system.

A single process of cleaning the coffee machine was known to us, but internally it implements multiple other processes that were actually abstracted from us. This is process abstraction in a nutshell.

Well, this process abstraction example really got me thinking of a very futuristic coffee machine!

Abstraction in Action

Now that we understand abstraction well, let’s see how we can implement it. Since I’ve spun my coffee stories so much already, let’s actually go ahead andimplement the coffee machine example in Java. You do the same in any other object-oriented programming language. The syntax might be a little bit different, but the general concept is the same.

Use abstraction to implement a coffee machine

Modern coffee machines have become pretty complex. Depending on your choice of coffee, they decide which of the available coffee beans to use and how to grind them. They also use the right amount of water and heat it to the required temperature to brew a huge cup of filter coffee or a small and strong espresso.

Implementing the CoffeeMachine abstraction

Using the concept of abstraction, you can hide all these decisions and processing steps within your CoffeeMachine class. If you want to keep it as simple as possible, you just need a constructor method that takes a Map of CoffeeBean objects to create a new CoffeeMachine object and a brewCoffee method that expects your CoffeeSelection and returns a Coffee object.

You can clone the source of the example project at https://github.com/thjanssen/Stackify-OopAbstraction.

import org.thoughts.on.java.coffee.CoffeeException;import java.utils.Map;public class CoffeeMachine { private Map<CoffeeSelection, CoffeeBean> beans; public CoffeeMachine(Map<CoffeeSelection, CoffeeBean> beans) { this.beans = beans } public Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException { Coffee coffee = new Coffee(); System.out.println(“Making coffee ...”); return coffee; }}

CoffeeSelection is a simple enum providing a set of predefined values for the different kinds of coffees.

public enum CoffeeSelection { FILTER_COFFEE, ESPRESSO, CAPPUCCINO;}

And the classes CoffeeBean and Coffee are simple POJOs (plain old Java objects) that only store a set of attributes without providing any logic.

public class CoffeeBean { private String name; private double quantity; public CoffeeBean(String name, double quantity) { this.name = name; this.quantity; }}
public class Coffee { private CoffeeSelection selection; private double quantity; public Coffee(CoffeeSelection, double quantity) { this.selection = selection; this. quantity = quantity; }}

Using the CoffeeMachine abstraction

Using the CoffeeMachine class is almost as easy as making your morning coffee. You just need to prepare a Map of the available CoffeeBeans. After that, instantiate a new CoffeeMachine object. Finally, call the brewCoffee method with your preferred CoffeeSelection.

import org.thoughts.on.java.coffee.CoffeeException;import java.util.HashMap;import java.util.Map;public class CoffeeApp { public static void main(String[] args) { // create a Map of available coffee beans Map<CoffeeSelection, CoffeeBean> beans = new HashMap<CoffeeSelection, CoffeeBean>(); beans.put(CoffeeSelection.ESPRESSO, new CoffeeBean("My favorite espresso bean", 1000)); beans.put(CoffeeSelection.FILTER_COFFEE, new CoffeeBean("My favorite filter coffee bean", 1000)); // get a new CoffeeMachine object CoffeeMachine machine = new CoffeeMachine(beans); // brew a fresh coffee try { Coffee espresso = machine.brewCoffee(CoffeeSelection.ESPRESSO);} catch(CoffeeException e) { e.printStackTrace(); } } // end main} // end CoffeeApp

You can see in this example that the abstraction provided by the CoffeeMachine class hides all the details of the brewing process. That makes it easy to use and allows each developer to focus on a specific class.

If you implement the CoffeeMachine, you don’t need to worry about any external tasks, like providing cups, accepting orders or serving the coffee. Someone else will work on that. Your job is to create a CoffeeMachine that makes good coffee.

And if you implement a client that uses the CoffeeMachine, you don’t need to know anything about its internal processes. Someone else already implemented it so that you can rely on its abstraction to use it within your application or system.

That makes the implementation of a complex application a lot easier. And this concept is not limited to the public methods of your class. Each system, component, class, and method provides a different level of abstraction. You can use that on all levels of your system to implement software that’s highly reusable and easy to understand.

Not limited to the client API

Let’s dive a little bit deeper into the coffee machine project and take a look at the constructor method of the CoffeeMachine class.

import java.util.Map;public class CoffeeMachine { private Map<CoffeeSelection, Configuration> configMap; private Map<CoffeeSelection, CoffeeBean> beans; private Grinder grinder; private BrewingUnit brewingUnit; public CoffeeMachine(Map<CoffeeSelection, CoffeeBean> beans) { this.beans = beans; this.grinder = new Grinder(); this.brewingUnit = new BrewingUnit(); // create coffee configuration this.configMap = new HashMap<CoffeeSelection, Configuration>(); this.configMap.put(CoffeeSelection.ESPRESSO, new Configuration(8, 28)); this.configMap.put(CoffeeSelection.FILTER_COFFEE, new Configuration(30, 480)); }}

As you can see in the code snippet, the constructor not only stores the provided Map of available CoffeeBeans in an internal property, it also initializes an internal Map that stores the configuration required to brew the different kinds of coffees and instantiates a Grinder and a BrewingUnit object.

OOP Concept for Beginners: What is Abstraction? - Stackify (3)

All these steps are not visible to the caller of the constructor method. The developer most likely doesn’t even know that the Grinder or BrewingUnit class exists. That’s another example of the abstraction that the CoffeeMachine class provides.

Each class provides its own abstraction

The classes Grinder and BrewingUnit provide abstractions on their own. The Grinder abstracts the complexity of grinding the coffee and BrewingUnit hides the details of the brewing process.

public class Grinder { public GroundCoffee grind(CoffeeBean coffeeBean, double quantityCoffee) { // ... }}
public class BrewingUnit { public Coffee brew(CoffeeSelection selection, GroundCoffee groundCoffee, double quantity) { // ... }}

That makes the implementation of the CoffeeMachine class a lot easier. You can implement the brewCoffee method without knowing any details about the grinding or brewing process. You just need to know how to instantiate the 2 classes and call the grind and brew methods.

Different abstraction levels within the same class

In this example, I took the abstraction one step further and implemented 3 methods to brew the different kinds of coffee. The brewCoffee method, which gets called by the client, just evaluates the provided CoffeeSelection and calls another method that brews the specified kind of coffee.

The brewFilterCoffee and brewEspresso methods abstract the specific operations required to brew the coffee.

private Coffee brewFilterCoffee() { Configuration config = configMap.get(CoffeeSelection.FILTER_COFFEE); // grind the coffee beans GroundCoffee groundCoffee = this.grinder.grind( this.beans.get(CoffeeSelection.FILTER_COFFEE), config.getQuantityCoffee()); // brew a filter coffee return this.brewingUnit.brew( CoffeeSelection.FILTER_COFFEE, groundCoffee, config.getQuantityWater());}
private Coffee brewEspresso() { Configuration config = configMap.get(CoffeeSelection.ESPRESSO); // grind the coffee beans GroundCoffee groundCoffee = this.grinder.grind( this.beans.get(CoffeeSelection.ESPRESSO), config.getQuantityCoffee()); // brew an espresso return this.brewingUnit.brew( CoffeeSelection.ESPRESSO, groundCoffee, config.getQuantityWater());}

I defined both methods as private because I just want to provide an additional, internal level of abstraction. That not only makes the implementation of the brewCoffee method a lot easier, it also improves the reusability of the code.

You could, for example, reuse the brewEspresso method when you want to support the CoffeeSelection.CAPPUCCINO. You would then just need to implement the required operations to heat the milk, call the brewEspresso method to get an espresso, and add it to the milk.

Abstraction vs Encapsulation

A lot of times programmers often confuse abstraction with encapsulation because in reality the two concepts are quite intertwined and share a relationship between them. Abstraction, as we’ve seen pertains to hiding underlying details and implementation in a program. Encapsulation, on the other hand, describes how abstraction occurs in a program.

Abstraction is a design-level process but encapsulation is an implementation process. Encapsulation tells us how exactly you can implement abstraction in the program. Abstraction pertains to only displaying the essential details to the user whereas encapsulation pertains to typing up all the data members and associated member functions into a single abstracted unit.

OOP Concept for Beginners: What is Abstraction? - Stackify (4)

Summary

Abstraction is a general concept which you can find in the real world as well as in OOP languages. Any objects in the real world that hide internal details provide an abstraction. The objects may be your coffee machine or classes in your current software project,

These abstractions make it a lot easier to handle complexity by splitting them into smaller parts. In the best case, you can use them without understanding how they provide the functionality. And that helps you to split the complexity of your next software project into manageable parts. It also enables you every morning to brew a fresh cup of amazing coffee while you’re still half asleep.

Looking to continually improve your applications? Most OOP languages are supported by Stackify’s free dynamic code profiler,Prefix, and Stackify’s full lifecycle APM, Retrace.Try both for free.

Related posts:

  • ASP.NET Interview Questions: Tips for Hiring ASP.NET Developers
  • What is AWS CLI? Understanding the Command Line Tool
  • Comparison: Node.js vs. PHP
  • 8 Benefits of Using AngularJS for Web App Development
  • Top 5 Python Memory Profilers

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

  • App Performance Management
  • Code Profiling
  • Error Tracking
  • Centralized Logging

Learn More

OOP Concept for Beginners: What is Abstraction? - Stackify (10)

Author

Thorben

More articles by Thorben

OOP Concept for Beginners: What is Abstraction? - Stackify (2024)

FAQs

OOP Concept for Beginners: What is Abstraction? - Stackify? ›

Abstraction is one of the key concepts of object-oriented programming (OOP) languages. Its main goal is to handle complexity by hiding unnecessary details from the user.

What are the 4 basics of OOP abstraction? ›

Objects contain data, referred to as attributes or properties, and methods. OOP allows objects to interact with each other using four basic principles: encapsulation, inheritance, polymorphism, and abstraction. These four OOP principles enable objects to communicate and collaborate to create powerful applications.

What is the simple definition of abstraction in OOP? ›

Abstraction in OOPS is used to hide unnecessary information and display only necessary information to the users interacting. It is essential to represent real-world objects in a simplified manner for users to interact easily. Let us consider a real-life situation where a man needs to withdraw cash from an ATM.

What is the OOP concept for beginners? ›

Object-Oriented Programming (OOP) is founded on a set of core principles that shape the design and construction of software systems. These principles — encapsulation, inheritance, abstraction, and polymorphism — serve as the pillars of OOP, offering a framework for crafting modular, adaptable, and maintainable code.

What are the 4 principles of OOP in C#? ›

In this article, we have explored the four fundamental pillars of object-oriented programming (OOP) in C#: Inheritance , Encapsulation , Polymorphism , and Abstraction . These pillars form the foundation of OOP and are essential concepts to understand when working with object-oriented programming languages like C#.

What is the difference between abstraction and encapsulation? ›

Abstraction is a design level process and it is used to reduce the complexity at the designing stage of a project. Encapsulation is an implementation level process, and it is used to provide privacy and maintain control over the transparency of data at the implementation stage of a project.

What is a real time example of abstraction? ›

Abstraction in Real Life

Your car is a great example of abstraction. You can start a car by turning the key or pressing the start button. You don't need to know how the engine is getting started, what all components your car has. The car internal implementation and complex logic is completely hidden from the user.

What is abstraction in your own words? ›

Abstraction (from the Latin abs, meaning away from and trahere , meaning to draw) is the process of taking away or removing characteristics from something in order to reduce it to a set of essential characteristics.

What is abstraction for dummies? ›

Abstraction is the process of "extracting the essence" of what we are working with and hiding or discarding what is not important or not relevant in order to simplify life or software. Cartoons are an example: only draw what is necessary to tell the story or convey intended meaning.

What is the best OOP language to learn first? ›

Which language is best for object-oriented programming? Java is one of the best and most widely used programming languages for OOP. Java has a large community with lots of resources and libraries, so it is easy for beginners to learn.

What are the 7 concepts of OOPs? ›

The seven object-oriented principles we've explored here (abstraction, encapsulation, polymorphism, inheritance, association, aggregation, and composition) can help you reuse your code, prevent security issues, and improve the performance of your Java applications.

What is OOP in layman terms? ›

Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior.

What is OOP abstraction? ›

Abstraction, in the context of OOP, refers to the ability to hide complex implementation details and show only the necessary features of an object. This simplifies the interaction with objects, making programming more intuitive and efficient.

What is polymorphism in simple words? ›

The word polymorphism is derived from Greek and means "having multiple forms." Apart from computer programming, the idea of polymorphism occurs in other real-world areas, including biology, chemistry and drug development. Polymorphism is one of the most important concepts in OOP.

What are the four basics of OOP? ›

There are 4 major principles that make an language Object Oriented. These are Encapsulation, Data Abstraction, Polymorphism and Inheritance. These are also called as four pillars of Object Oriented Programming.

What are the 4 types of abstraction? ›

Types of Abstraction:
  • Data abstraction – This type only shows the required information about the data and hides the unnecessary data.
  • Control Abstraction – This type only shows the required information about the implementation and hides unnecessary information.
Sep 6, 2023

What are the 4 main concepts of object-oriented programming? ›

The main ideas behind Java's Object-Oriented Programming, OOP concepts include abstraction, encapsulation, inheritance and polymorphism.

What are the 4 factors of OOP? ›

The Four pillars of OOPs, abstraction, encapsulation, inheritance, and polymorphism, are integral to understanding and using OOP. By using these four pillars of OOPs, developers can create programs that are powerful, maintainable, and extensible, making them a great choice for software development.

What are the 4 basic methods in object-oriented programming? ›

There are four fundamental concepts of Object-oriented programming – Inheritance, Encapsulation, Polymorphism, and Data abstraction. It is essential to know about all of these in order to understand OOPs.

Top Articles
Latest Posts
Article information

Author: Tish Haag

Last Updated:

Views: 5863

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Tish Haag

Birthday: 1999-11-18

Address: 30256 Tara Expressway, Kutchburgh, VT 92892-0078

Phone: +4215847628708

Job: Internal Consulting Engineer

Hobby: Roller skating, Roller skating, Kayaking, Flying, Graffiti, Ghost hunting, scrapbook

Introduction: My name is Tish Haag, I am a excited, delightful, curious, beautiful, agreeable, enchanting, fancy person who loves writing and wants to share my knowledge and understanding with you.