Last Updated : 26 Jul, 2025
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 AbstractionA 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?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())
Explanation:
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 MethodAbstract 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 MethodConcrete 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 PropertiesAbstract 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)
Explanation:
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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4