Python Abstraction

Introduction

Abstraction is one of the key concepts of object-oriented programming (OOP). It is the process of hiding the complex implementation details and showing only the necessary features of an object. This concept helps in reducing programming complexity and effort, by allowing the programmer to focus on interactions at a higher level without worrying about the implementation details.

Key Concepts

Abstract Class

An abstract class is a class that cannot be instantiated on its own and is meant to be subclassed. It typically contains one or more abstract methods that must be implemented by subclasses.

Abstract Method

An abstract method is a method that is declared in an abstract class but does not contain any implementation. Subclasses are required to provide an implementation for these methods.

Syntax

Defining an Abstract Class

Abstract classes and methods are defined using the abc module in Python.

from abc import ABC, abstractmethod

class AbstractClassName(ABC):
    @abstractmethod
    def abstract_method(self):
        pass

Example

from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

class Dog(Animal):
    def sound(self):
        return "Bark"

class Cat(Animal):
    def sound(self):
        return "Meow"

# Creating objects of the subclasses
dog = Dog()
cat = Cat()

print(dog.sound())  # Output: Bark
print(cat.sound())  # Output: Meow

Real-World Example: Banking System

Let’s consider a real-world example of a banking system where we have different types of bank accounts.

Abstract Class: BankAccount

from abc import ABC, abstractmethod

class BankAccount(ABC):
    def __init__(self, account_number, balance):
        self.account_number = account_number
        self.balance = balance

    @abstractmethod
    def account_type(self):
        pass

    def deposit(self, amount):
        self.balance += amount
        return f"Deposited {amount}. New balance is {self.balance}"

    def withdraw(self, amount):
        if amount <= self.balance:
            self.balance -= amount
            return f"Withdrew {amount}. New balance is {self.balance}"
        else:
            return "Insufficient balance"

Subclass: SavingsAccount

class SavingsAccount(BankAccount):
    def account_type(self):
        return "Savings Account"

# Creating an object of the SavingsAccount class
savings = SavingsAccount("1234567890", 10000)

# Using methods from the abstract class and the subclass
print(savings.account_type())  # Output: Savings Account
print(savings.deposit(2000))  # Output: Deposited 2000. New balance is 12000
print(savings.withdraw(5000))  # Output: Withdrew 5000. New balance is 7000

Subclass: CheckingAccount

class CheckingAccount(BankAccount):
    def account_type(self):
        return "Checking Account"

# Creating an object of the CheckingAccount class
checking = CheckingAccount("0987654321", 20000)

# Using methods from the abstract class and the subclass
print(checking.account_type())  # Output: Checking Account
print(checking.deposit(5000))  # Output: Deposited 5000. New balance is 25000
print(checking.withdraw(3000))  # Output: Withdrew 3000. New balance is 22000

Explanation

  1. Abstract Class (BankAccount): Defines the common interface for different types of bank accounts. It includes an abstract method account_type() and concrete methods deposit() and withdraw().
  2. Concrete Subclasses (SavingsAccount and CheckingAccount): Implement the abstract method account_type() and inherit the concrete methods from BankAccount.

Conclusion

Abstraction in Python helps to hide the complex implementation details and expose only the necessary parts of an object. By using abstract classes and methods, you can create a clear and understandable interface for your objects, which can be implemented in different ways by subclasses. This not only enhances code readability but also promotes code reusability and maintainability. The real-world example of a banking system demonstrates how abstraction can be applied to manage different types of bank accounts effectively.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top