Abstraction is a core concept in object-oriented programming (OOP) that helps in simplifying complex systems by hiding the internal details and exposing only the necessary functionalities. It allows users to interact with an object or system at a higher level without needing to understand its internal workings.
Abstraction can be understood with the following examples:
1. Smartphone: When using a smartphone, you interact with applications like the camera or voice recorder. You don’t need to understand the complex underlying processes or algorithms to use these features.
2. TV Remote: When you increase the volume using a TV remote, you press the "+" button. You don't need to know how the remote communicates with the TV or how the TV adjusts the volume. You only need to know the action of pressing the button.
This level of interaction where the user only needs to understand what the system does, rather than how it does it, is the essence of abstraction.
1. Reduces Complexity: By hiding unnecessary details, abstraction makes complex systems easier to manage and understand.
2. Enhances Efficiency: Abstraction helps in focusing on interactions rather than implementation, leading to more efficient code development and maintenance.
3. Improves Code Reusability: Abstract classes and methods allow different implementations to be used interchangeably.
In Python, abstraction is achieved using abstract classes and methods. An abstract class provides a blueprint for other classes, and abstract methods are methods that are declared but contain no implementation.
An abstract class is a class that cannot be instantiated on its own and must be subclassed. It can contain both abstract methods (methods without implementation) and concrete methods (methods with implementation).
1. Import the abc module: This module provides the infrastructure for defining abstract base classes.
2. Create an Abstract Base Class (ABC): Define a class that inherits from ABC (Abstract Base Class).
3. Use the @abstractmethod decorator: Mark methods that should be abstract.
from abc import ABC, abstractmethod
class AbstractClass(ABC):
@abstractmethod
def abstract_method(self):
pass
def concrete_method(self):
print("This is a concrete method.")
In this example:
• AbstractClass is an abstract class.
• abstract_method is an abstract method with no implementation.
• concrete_method is a concrete method with an implementation.
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return 2 * (self.width + self.height)
# Creating an instance of Rectangle
rect = Rectangle(5, 10)
print("Area:", rect.area()) # Output: Area: 50
print("Perimeter:", rect.perimeter()) # Output: Perimeter: 30
In this example:
• Shape is an abstract class with two abstract methods: area and perimeter.
• Rectangle is a concrete class that inherits from Shape and provides implementations for the abstract methods.
1. Abstract Methods: Methods declared in an abstract class with the @abstractmethod decorator must be overridden in any concrete subclass.
2. Abstract Classes: Cannot be instantiated directly. They serve as a template for other classes.
3. Concrete Methods: Abstract classes can also have methods with implementations that can be used by subclasses.