B3.2.3
Explain the concept of abstraction in OOP
Abstraction means exposing what an object does while hiding how it does it (hiding the complex internal implementation details) . Users can operate an…
What Is Abstraction?
Abstraction means exposing what an object does while hiding how it does it (hiding the complex internal implementation details). Users can operate an object correctly without knowing internal implementation details.
Real-world analogy (car):
- Press the accelerator, the car moves.
- Press the brake, the car slows down.
- The driver does not need to know the engine/control-system internals.
Why Use Abstraction?
| Benefit | Explanation |
|---|---|
| Reduced complexity | Users interact with essential interfaces only |
| Improved maintainability | Internal implementations can change without breaking external code (if the interface remains stable) |
| Enforced contract | Parent abstract class defines required methods for subclasses |
| Better reusability | Different concrete objects can be handled through a common abstract type |
Abstract Classes and Abstract Methods
An abstract class is used to define a shared blueprint:
- It can contain fields and concrete methods
- It can contain abstract methods (declared but not implemented)
- An abstract class cannot be instantiated directly
- Subclasses must implement all abstract methods (or remain abstract)
Abstraction vs Encapsulation
| Concept | Focus | Key Question |
|---|---|---|
| Abstraction | Hide implementation, expose essential behavior | “What can this object do?” |
| Encapsulation | Bundle data/behavior and control access | “What can be accessed directly?” |
Dynamic Binding and Abstraction
When code uses an abstract parent reference, runtime dispatch binds calls to the concrete subclass implementation. Simple view: compile-time checks method availability in the abstract contract; runtime decides which subclass version executes.
Python Example: abc Abstract Class
1. Define an Abstract Class and Subclasses
from abc import ABC, abstractmethod
class Animal(ABC):
def __init__(self, species: str, habitat: str):
self.species = species
self.habitat = habitat
@abstractmethod
def move(self) -> str:
"""Subclasses must implement movement behavior."""
pass
@abstractmethod
def sound(self) -> str:
pass
class Lion(Animal):
def move(self) -> str:
return "Lion runs on land"
def sound(self) -> str:
return "Roar"
class Fish(Animal):
def move(self) -> str:
return "Fish swims in water"
def sound(self) -> str:
return "Blub"
2. Abstract Classes Cannot Be Instantiated
# a = Animal("Unknown", "Unknown")
# TypeError: Can't instantiate abstract class Animal ...
3. Use One Interface for Different Subclasses
animals = [Lion("Lion", "Savanna"), Fish("Fish", "Ocean")]
for a in animals:
print(a.move(), "-", a.sound())
Common Mistakes (Exam Focus)
- Abstract class is not exactly the same as an interface (many languages allow fields/concrete methods in abstract classes).
- Abstract methods must be implemented by concrete subclasses.
- Abstract classes cannot be instantiated directly.
- Do not confuse abstraction with access modifiers (
private/protected/public), which belong to encapsulation.