B3.2.3 Explain the concept of abstraction in OOP.
• The significance of abstraction in the development of modular code fragments
• The use of abstract classes to establish common interfaces for sub-classes
The big idea
In object-oriented programming, abstraction is about focusing on what an object does rather than how it does it.
When you use a car, you interact with the steering wheel, pedals, and gear shift — you don’t need to know the details of the engine, the brake hydraulics, or the transmission internals.
In programming, abstraction lets us:
- Hide unnecessary details
- Present a clear, simplified interface
- Allow different implementations to share the same high-level behavior
Why abstraction matters
- Modularity: Breaks a system into self-contained components that interact through well-defined interfaces.
- Reusability: Common interfaces mean different classes can be used interchangeably.
- Maintainability: Implementation details can change without affecting the rest of the system.
- Scalability: Adding new types of objects is easier because the core interface stays the same.
Abstract classes
An abstract class is a class that:
- Can define abstract methods — methods with no body, just a declaration.
- Can contain concrete methods — fully implemented methods.
- Cannot be instantiated directly — you must create subclasses that implement the abstract methods.
Purpose:
Abstract classes define a common contract for subclasses while leaving details to be filled in.
Example (Java)
// Abstract class: cannot be instantiated
public abstract class Shape {
// Abstract method: no implementation here
public abstract double area();
// Concrete method: all shapes can use this
public void describe() {
System.out.println("I am a shape.");
}
}
public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
// Implement the abstract method
public double area() {
return Math.PI * radius * radius;
}
}
public class Rectangle extends Shape {
private double width, height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double area() {
return width * height;
}
}
Example (Python with abc module)
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
def describe(self):
print("I am a shape.")
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
How abstraction supports modular design
Imagine a graphics program that works with many shapes — Circle, Rectangle, Triangle, etc. If all shapes share the same abstract class Shape:
- The program can store them in one list:
List<Shape> shapes - It can call
shape.area()on each, without caring about the exact type. - Adding a new
Polygonclass requires no changes to the code that uses shapes.
Key benefits in real projects
- Decoupling: High-level modules depend only on abstract definitions, not concrete implementations.
- Extensibility: New subclasses can be added without altering existing code.
- Team development: Different developers can work on different subclasses while agreeing on the same abstract contract.