Open In App

Data Abstraction in Python

Last Updated : 26 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Data abstraction means showing only the essential features and hiding the complex internal details.

Technically, in Python abstraction is used to hide the implementation details from the user and expose only necessary parts, making the code simpler and easier to interact with.

Example of Data Abstraction

A smartphone is a great real-life example of data abstraction you can make calls or take photos without knowing how signals or storage work. Only essential features are shown, complex details are hidden.

Why do we need Data Abstraction?

  • It hides internal logic and only shows the necessary details, making it easier to use complex systems.
  • Sensitive or unnecessary details are not exposed, reducing chances of misuse or accidental changes.
  • Users can focus on what the object does instead of how it does it.
  • Internal changes don’t affect external code, making it easier to update or modify code components.

Abstract Base Class

In Python, an Abstract Base Class (ABC) is used to achieve data abstraction by defining a common interface for its subclasses. It cannot be instantiated directly and serves as a blueprint for other classes.

Abstract classes are created using abc module and @abstractmethod decorator, allowing developers to enforce method implementation in subclasses while hiding complex internal logic.

Example:

Python
from abc import ABC, abstractmethod

class Greet(ABC):
    @abstractmethod
    def say_hello(self):
        pass  # Abstract method

class English(Greet):
    def say_hello(self):
        return "Hello!"

g = English()
print(g.say_hello()) 

Output
Hello!

Explanation:

  • Greet is an abstract class with a method say_hello() that has no implementation.
  • English implements this method and returns a greeting.
  • This keeps structure fixed while letting subclasses define their own behavior.

Components of Abstraction

Abstraction in Python is made up of key components like abstract methods, concrete methods, abstract properties and class instantiation rules. These elements work together to define a clear and enforced structure for subclasses while hiding unnecessary implementation details. Let's discuss them one by one.

Abstract Method

Abstract methods are method declarations without a body defined inside an abstract class. They act as placeholders that force subclasses to provide their own specific implementation, ensuring consistent structure across derived classes.

Example:

Python
from abc import ABC, abstractmethod
class Animal(ABC):
    @abstractmethod
    def make_sound(self):
        pass  # Abstract method, no implementation here

Explanation: make_sound() is an abstract method in Animal class, so it doesn't have any code inside it.

Concrete Method

Concrete methods are fully implemented methods within an abstract class. Subclasses can inherit and use them directly, promoting code reuse without needing to redefine common functionality.

Example:

Python
from abc import ABC, abstractmethod
class Animal(ABC):
    @abstractmethod
    def make_sound(self):
        pass  # Abstract method, to be implemented by subclasses

    def move(self):
        return "Moving"  # Concrete method with implementation

Explanation: move() method is a concrete method in Animal class. It is implemented and does not need to be overridden by Dog class.

Abstract Properties

Abstract properties work like abstract methods but are used for properties. These properties are declared with @property decorator and marked as abstract using @abstractmethod. Subclasses must implement these properties.

Example:

Python
from abc import ABC, abstractmethod

class Animal(ABC):
    @property
    @abstractmethod
    def species(self):
        pass  # Abstract property, must be implemented by subclasses

class Dog(Animal):
    @property
    def species(self):
        return "Canine"

# Instantiate the concrete subclass
dog = Dog()
print(dog.species)

Output
Canine

Explanation:

  • species is an abstract property in Animal class and it is marked as @abstractmethod.
  • Dog class implements species property, making it a concrete subclass that can be instantiated.
  • Abstract properties enforce that a subclass provides property’s implementation.

Abstract Class Instantiation

Abstract classes cannot be instantiated directly. This is because they contain one or more abstract methods or properties that lack implementations. Attempting to instantiate an abstract class results in a TypeError.

Example:

Python
from abc import ABC, abstractmethod

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

animal = Animal() 

Explanation:

  • Animal class is abstract because it has make_sound() method as an abstract method.
  • Instantiating Animal() raises a TypeError because abstract classes with unimplemented methods can't be instantiated, only fully implemented subclasses can.

Abstraction in Python
Visit Course explore course icon
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy