DEV Community

Cover image for Abstraction in Object Oriented Programming(OOP) For Beginners
BiKodes
BiKodes

Posted on • Edited on

Abstraction in Object Oriented Programming(OOP) For Beginners

Abstraction is a crucial concept of Object Oriented Programming (OOP) in the field of computer science that is used to make code more manageable, secure, and efficient.

It is a technique that involves hiding the implementation details of a class and only exposing the essential features of the class to the user.This allows the user to work with objects of the class without having to worry about the details of how the class works.

In the most simplest way abstraction is hiding the unnecessary details of an object while exposing only the relevant aspects. In a snapshot, it is a process where the programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency.

Abstraction helps a software developer to define abstract data types. These are classes representing real-world entities, their properties (attributes), and their actions (methods). They also allow us to hide the internal complexities and implementation details of an object from the outside world.

Example One:

Let’s say you have a class called Car that has methods such as start_engine(), accelerate(), and brake(). The user only needs to know that they can start the engine, accelerate, and brake the car, but they don't need to know how these methods are implemented.

Here's what the code would look like:

class Car:
    def start_engine(self):
        # Implementation details hidden
        print("Engine started")

    def accelerate(self):
        # Implementation details hidden
        print("Accelerating")

    def brake(self):
        # Implementation details hidden
        print("Braking")
Enter fullscreen mode Exit fullscreen mode

In the example above, the user can use the Car class in a straightforward way, without having to worry about the implementation details. Someone else worried about that, and a car now acts as an abstraction and hides these details. The user just interacts with a simple interface that doesn't require any knowledge about the internal implementation.

Advantages of abstraction

  • Reduces complexity

  • Increase re-usability

  • Increases security

  • Loose coupling

  • Avoids code duplication

  • Manageable code

  • Code efficiency

There are two types of abstraction in OOP:

  1. Data abstraction
  2. Process abstraction
  • Data abstraction (Object Properties)

Data abstraction is like looking at a car from the outside. You don't need to know how the engine works but you only see the essential things such as its color, model, and speed.

In programming, it's about creating a clear and simple "container" for information about something, like a person, a car, a house et cetera.

The container hides the complicated stuff and gives you easy ways to see and change the significant information.

Example:

Think of a Person class in a program. It could have attributes like name, age, and address. These attributes are like the simple, visible parts of a person.

Data abstraction makes sure that a developer can get and set these attributes without worrying about how they are stored or processed inside the program.

  • Procedural Abstraction(Actons/Functions)

Procedural abstraction is like a magic button. When you press it, you don't need to know how the magic happens, you just know it will do something you want.

In programming, it is about creating easy-to-use magic buttons (functions or methods) that do specific tasks without showing you the complicated steps they take.

Example:

Think of a calculate_are function. The user gives it the size of a shape, and it magically tells you the area without you having to know the math behind it.

Procedural abstraction hides the complexity and lets you use these functions without understanding how they work internally.

Example Two:

The following code demonstrates abstraction by defining an abstract class Shape with abstract methods area() and perimeter(). The Circle and Square classes inherit from the Shape class and provide their own implementations for the abstract methods.

This way, the abstract class Shape defines a blueprint for shapes, enforcing that any class inheriting from it must implement these methods, ensuring a level of abstraction and structure in the design.

from abc import ABC, abstractmethod

# Abstract class
class Shape(ABC):
    def __init__(self, name):
        self.name = name

    @abstractmethod
    def area(self):
        pass

    @abstractmethod
    def perimeter(self):
        pass

# Concrete classes implementing Shape
class Circle(Shape):
    def __init__(self, name, radius):
        super().__init__(name)
        self.radius = radius

    def area(self):
        return 3.14 * self.radius * self.radius

    def perimeter(self):
        return 2 * 3.14 * self.radius

class Square(Shape):
    def __init__(self, name, side):
        super().__init__(name)
        self.side = side

    def area(self):
        return self.side * self.side

    def perimeter(self):
        return 4 * self.side

# Creating objects and using abstraction
circle = Circle("Circle", 5)
square = Square("Square", 4)

print(f"Area of {circle.name}: {circle.area()}")
print(f"Perimeter of {circle.name}: {circle.perimeter()}")

print(f"Area of {square.name}: {square.area()}")
print(f"Perimeter of {square.name}: {square.perimeter()}")

Enter fullscreen mode Exit fullscreen mode

Conclusion

Abstraction is one of the core 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.

Both types of abstraction simplify programming by focusing on what's important (the data or the actions) and hiding the rest (the details). This makes your code easier to understand, maintain, and reuse.Above all they make it a lot easier to handle complexity by splitting them into smaller parts.

Top comments (0)