Abstraction in object-oriented programming (OOP) means representing only the essential features of an entity while hiding unnecessary implementation details. An abstract class is a class designed to provide a common structure for subclasses; it cannot itself be instantiated.
Abstraction reduces complexity by separating what an object does from how it does it. For example, a program can call an object's calculateArea() method without needing to know the algorithm used for each shape.
An abstract class supports this process through inheritance. It can contain:
- Abstract methods, which declare required behaviour but provide no implementation.
- Concrete methods, which include an implementation inherited by subclasses.
- Attributes and constructors shared by related subclasses.
For example:
ABSTRACT CLASS Shape
ABSTRACT METHOD calculateArea()
METHOD displayType()
OUTPUT "This is a shape"
END METHOD
END CLASS
CLASS Circle INHERITS Shape
METHOD calculateArea()
RETURN pi * radius * radius
END METHOD
END CLASS
Shape defines the general concept, but creating a Shape object is not permitted because its area cannot be calculated without a specific shape. Circle supplies the missing implementation, so a Circle object can be instantiated. Other subclasses, such as Rectangle, can implement the same method differently, enabling polymorphism.
| Concept | Purpose |
|---|---|
| Abstraction | Hides unnecessary detail and exposes essential behaviour. |
| Abstract class | Provides a non-instantiable superclass containing shared structure and behaviour. |
| Abstract method | Requires subclasses to provide a specific implementation. |
| Concrete subclass | Implements required abstract methods and can be instantiated. |
A common misconception is that abstraction and encapsulation are identical. Abstraction manages complexity by presenting essential features; encapsulation bundles data and methods while restricting direct access to an object's internal state.
For IB Computer Science HL B3.2, an examiner may ask you to describe an abstract class or explain its role in a multi-class design. State that it cannot be instantiated, identify abstract methods, and explain how subclasses implement them to support inheritance and polymorphism.