Python Topics : Data Abstraction
Importance of Data Abstraction
data abstraction is one of the most essential concepts of OOP
enables programmers to hide complex implementation details while just showing users the most crucial data and functions
abstraction makes it
  • easier to design modular and well-organized code
  • makes code simpler to understand and maintain
  • promotes code reuse
  • improves developer collaboration
Abstract Classes
achieve data abstraction by using abstract classes
abstract classes can be created using abc (abstract base class) module and abstractmethod of abc module

Abstract Method
abstract method feature is not a default feature.
to create abstract method and abstract classes need to import the "ABC" and "abstractmethod" classes from abc (Abstract Base Class) library
abstract method of base class forces its child class to write the implementation of the all abstract methods defined in base class
below code foo is a abstract method created using @abstractmethod decorator
from abc import ABC, abstractmethod

class BaseClass(ABC):
    @abstractmethod
    def foo(self):
         #empty body
         pass
Concrete Method
concrete methods are the methods defined in an abstract base class with their complete implementation
concrete methods are required to avoid reprication of code in subclasses
in abstract base class there may be a method whose implementation is to be same in all its subclasses
below startEngine is a concrete method
class Car(ABC):
    def __init__(self, brand, model, year):
        self.brand = brand
        self.model = model
        self.year = year
        self.engine_started = True

    def startEngine(self):
        if not self.engine_started:
            print(f"Starting the {self.model}'s engine.")
            self.engine_started = True
        else:
            print("Engine is already running.")
Steps to Create Abstract Base Class and Abstract Method
  1. import ABC and abstractmethod class from abc library
  2. create a BaseClass that inherits from the ABC class
    when a class inherits from ABC, it indicates that the class is intended to be an abstract base class
  3. in the BaseClass declare an abstract method named "foo" by using "abstractmethod" decorater
    any subclass derived from BaseClass must implement the foo method
    write pass in this method which indicates that there is no code or logic in this method
from abc import ABC, abstractmethod

class BaseClass(ABC):
    @abstractmethod
    def foo(self):
        #empty body
        pass
Data Abstraction Implementation
in the below code data abstraction is implemented using abstract class and method
import the required modules or classes from abc library
create a base class 'Car' that inherited from 'ABC' class
in the base class create the init function, the abstract function and non-abstract functions
to declare abstract function printDetails we use @abstractmethod decorator
create child class hatchback and suv
child classes inherited from abstract class so need to write the implementation of all abstract function declared in the base class
write the implementation of abstract method in both child classes
create an instance of a child class and call the printDetails method
# Import required modules
from abc import ABC, abstractmethod

# Create Abstract base class
class Car(ABC):
    def __init__(self, brand, model, year):
        self.brand = brand
        self.model = model
        self.year = year
    
    # Create abstract method      
    @abstractmethod
    def printDetails(self):
        pass
  
    # Create concrete method
    def accelerate(self):
        print("Speed up ...")
  
    def break_applied(self):
        print("Car stopped")

# Create a child class
class Hatchback(Car):
    def printDetails(self):
        print("Brand:", self.brand)
        print("Model:", self.model)
        print("Year:", self.year)
  
    def sunroof(self):
        print("Not having this feature")

# Create a child class
class Suv(Car):
    def printDetails(self):
        print("Brand:", self.brand)
        print("Model:", self.model)
        print("Year:", self.year)
  
    def sunroof(self):
        print("Available")

# Create an instance of the Hatchback class
car1 = Hatchback("Maruti", "Alto", "2022")

# Call methods
car1.printDetails()
car1.accelerate()
car1.sunroof()
index