Abstraction
Part of Object-Oriented Programming
What is Abstraction?
Abstraction is the concept of hiding complex implementation details and showing only the necessary features of an object. It focuses on what an object does rather than how it does it.
Through abstraction, you create a simplified interface that users can interact with, without needing to understand the underlying complexity.
Real-World Analogy
When you drive a car, you use a simple interface: steering wheel, accelerator, brake. You don't need to understand the combustion engine, fuel injection, or transmission systems. The complexity is abstracted away - you just press the gas and the car moves!
Interactive Visualization
Abstraction: Simple Interface, Complex Implementation
Abstraction vs Encapsulation
Abstraction
- • Hides complexity
- • Design-level concept
- • Focuses on what to show
- • Achieved via abstract classes/interfaces
- • Example: Car interface (start, stop, drive)
Encapsulation
- • Hides data
- • Implementation-level concept
- • Focuses on how to protect
- • Achieved via access modifiers
- • Example: Private engine temperature variable
Abstract Classes vs Interfaces
| Feature | Abstract Class | Interface |
|---|---|---|
| Methods | Can have both abstract and concrete | Only abstract (traditionally) |
| Variables | Can have instance variables | Only constants (static final) |
| Inheritance | Single inheritance | Multiple interfaces allowed |
| Constructor | Can have constructors | Cannot have constructors |
| Use when | Classes share common code | Defining a contract/capability |
Code Implementation
# Abstraction in Python using ABC (Abstract Base Class)
from abc import ABC, abstractmethod
# Abstract class - cannot be instantiated directly
class Vehicle(ABC):
def __init__(self, brand):
self.brand = brand
# Abstract methods - MUST be implemented by subclasses
@abstractmethod
def start(self):
pass
@abstractmethod
def stop(self):
pass
@abstractmethod
def accelerate(self):
pass
# Concrete method - can be used as-is
def honk(self):
return f"{self.brand} goes beep beep!"
# Concrete class implementing the abstraction
class Car(Vehicle):
def __init__(self, brand, fuel_type):
super().__init__(brand)
self.fuel_type = fuel_type
self._engine_temp = 0 # Hidden detail
def start(self):
# Complex internal process hidden
self._check_fuel()
self._initialize_engine()
self._engine_temp = 90
return f"{self.brand} car started!"
def stop(self):
self._engine_temp = 0
return f"{self.brand} car stopped!"
def accelerate(self):
return f"{self.brand} car accelerating..."
# Hidden implementation details
def _check_fuel(self):
print("Checking fuel level...")
def _initialize_engine(self):
print("Initializing engine systems...")
class ElectricBike(Vehicle):
def __init__(self, brand, battery_capacity):
super().__init__(brand)
self.battery = battery_capacity
def start(self):
return f"{self.brand} e-bike powered on!"
def stop(self):
return f"{self.brand} e-bike powered off!"
def accelerate(self):
return f"{self.brand} e-bike accelerating silently..."
# Using abstraction - same interface, different implementations
def test_drive(vehicle: Vehicle):
print(vehicle.start())
print(vehicle.accelerate())
print(vehicle.honk())
print(vehicle.stop())
# Can't create Vehicle directly
# v = Vehicle("Generic") # TypeError!
# Create concrete implementations
car = Car("Toyota", "Gasoline")
bike = ElectricBike("Tesla", "100kWh")
test_drive(car)
test_drive(bike)Common Mistakes
Forgetting to implement all abstract methods
If you inherit from an abstract class, you must implement ALL abstract methods. Missing even one will cause a compilation error (or runtime error in Python).
Over-abstraction
Don't create abstract classes for everything. If you only have one implementation, an abstract class might be unnecessary complexity. Use abstraction when you have multiple implementations.
Trying to instantiate abstract classes
Abstract classes cannot be instantiated directly. You must create a concrete subclass that implements all abstract methods.
Interview Tips
Test Your Knowledge
Abstraction Quiz
Question 1 of 6What is abstraction in OOP?