Skip to content
Learnearn.uk » Home » Abstraction

Abstraction

Introduction

Introduction to Abstraction

Abstraction is the process of simplifying complex concepts or systems by focusing on the most important aspects while hiding unnecessary details. It involves creating higher-level representations or models that capture the essential elements or behaviors, allowing for easier understanding, communication, and problem-solving.

Abstraction in programming can be achieved through a number of techniques including:

  • Data Abstraction
  • Encapsulation
  • Function Abstraction
  • Modularisation

Data Abstraction

Data Abstraction

Data abstraction involves representing complex data structures or entities in a simplified manner by encapsulating them into classes or objects. It allows programmers to define abstract data types (ADTs) that hide the implementation details and expose only essential operations or behaviors.

Example

Python contains a number of in-built data structures, such as lists and dictionaries, that abstract away the unnecessary details and complexity. In the example below we are only storing the name of the animal, because all other details are irrelevant.

animals = []
animals.append("dog")
animals.append("cat")
print(animals)

The specific details of the implementation are hidden – for example we don’t know (and don’t care) where in memory the data is stored, or how it is encoded.

Encapsulation

Encapsulation

Encapsulation is the practice of bundling data and related operations together into a single unit, typically known as a class. It allows for information hiding, where the internal details of an object or module are inaccessible to external entities, promoting modularity and preventing unintended dependencies.

Example

class Animal:
    def __init__(self, name):
        self.name = name
    
    def make_sound(self):
        pass

class Dog(Animal):
    def make_sound(self):
        return "Woof!"

class Cat(Animal):
    def make_sound(self):
        return "Meow!"

# Creating objects of the Dog and Cat classes
dog = Dog("Buddy")
cat = Cat("Whiskers")

# Accessing the make_sound method of the objects
print(dog.name + ": " + dog.make_sound())
print(cat.name + ": " + cat.make_sound())

Functions

Function Abstraction

Function abstraction involves encapsulating a set of instructions or operations into a function or method. It allows for code reuse, improved readability, and separation of concerns by providing a named abstraction for a specific task or behavior.

import math

# Abstraction for calculating the area of different shapes

def calculate_area(shape, *args):
    if shape == "circle":
        radius = args[0]
        area = math.pi * radius ** 2
        return area
    elif shape == "rectangle":
        length = args[0]
        width = args[1]
        area = length * width
        return area
    elif shape == "triangle":
        base = args[0]
        height = args[1]
        area = 0.5 * base * height
        return area
    else:
        return None

# Calculating the areas of different shapes

circle_area = calculate_area("circle", 5)
rectangle_area = calculate_area("rectangle", 4, 6)
triangle_area = calculate_area("triangle", 3, 8)

# Displaying the areas

print("Circle area:", circle_area)
print("Rectangle area:", rectangle_area)
print("Triangle area:", triangle_area)

 

Modularisation

Abstraction through Modularisation

Modularization involves breaking down a program into smaller, independent modules or components. Each module focuses on a specific task or responsibility, hiding the internal details and providing well-defined interfaces for interaction with other modules. This promotes code reusability, maintainability, and scalability.

circle.py

import math

def calculate_area(radius):
    return math.pi * radius ** 2

def calculate_circumference(radius):
    return 2 * math.pi * radius

main.py

from circle import calculate_area, calculate_circumference

radius = float(input("Enter the radius of the circle: "))

area = calculate_area(radius)
circumference = calculate_circumference(radius)

print("Area of the circle:", area)
print("Circumference of the circle:", circumference)

In the example above the code responsible for calculating the area and circumference of a circle is moved to its own module. This makes the main.py code more readable as well as providing other benefits.