Polymorphism is an object-oriented programming principle that allows objects of different classes to be treated through the same interface — specifically, the same method name — while each class provides its own implementation of what that method does. The word comes from Greek: poly (many) + morphe (form) — many forms, one name.
Why is polymorphism useful?
Consider a drawing application with three shape classes — Circle, Rectangle, and Triangle. Each shape needs to be drawn on screen, but the drawing calculation is completely different for each. Without polymorphism, you would need separate method names: draw_circle(), draw_rectangle(), draw_triangle(). With polymorphism, every shape has a draw() method; the programmer calls draw() on any shape without knowing or caring which class it belongs to.
This makes programs far more flexible and extensible. Adding a new shape class only requires writing a draw() method for that class — nothing else in the program needs to change.
How does polymorphism work in Python?
Python achieves polymorphism through method overriding — a subclass redefines a method inherited from a parent class with its own version.
class Animal:
def speak(self):
return "..."
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
class Duck(Animal):
def speak(self):
return "Quack!"
All three subclasses override the speak() method from Animal. Now:
animals = [Dog(), Cat(), Duck()]
for animal in animals:
print(animal.speak())
Output:
Woof!
Meow!
Quack!
The same animal.speak() call produces different output depending on the type of object stored in animal. The loop does not know or care whether it has a Dog, Cat, or Duck — it just calls speak() and each object responds in its own way.
What is method overriding?
Method overriding occurs when a subclass defines a method with exactly the same name as a method in its parent class. When the method is called on an instance of the subclass, Python uses the subclass's version rather than the parent's.
Key rules for method overriding:
| Rule | Explanation |
|---|---|
| Same method name | The subclass method must have the exact same name as the parent method |
| Same or compatible parameters | The parameter list should be compatible (at GCSE, usually identical) |
| Different body | The body of the subclass method provides the specialised behaviour |
| Parent method still exists | The parent class method is not deleted; it can still be called using super() |
How does polymorphism relate to inheritance?
Polymorphism and inheritance work hand in hand in object-oriented programming:
- Inheritance establishes the parent–child relationship and lets the subclass inherit attributes and methods from the parent.
- Polymorphism allows the child to specialise inherited methods with its own behaviour.
Without inheritance, there is no parent method to override. The combination means you can write code that works with any object of the parent type, and each child object automatically provides the correct, specialised behaviour.
Hierarchy for the animal example:
Animal (parent)
├── Dog (child — overrides speak())
├── Cat (child — overrides speak())
└── Duck (child — overrides speak())
What is the difference between polymorphism and encapsulation?
These are two separate OOP principles that work alongside each other:
| Concept | Definition | Example |
|---|---|---|
| Polymorphism | Same method name, different behaviour depending on the object's class | speak() returns "Woof" for Dog but "Meow" for Cat |
| Encapsulation | Hiding internal data behind methods; private attributes accessed through getters and setters | _age is private; accessed via get_age() |
| Inheritance | A child class acquires the attributes and methods of a parent class | Dog inherits name attribute from Animal |
| Abstraction | Hiding complex implementation details; exposing only what is necessary | Calling draw() without knowing the maths behind it |
Frequently asked questions
What is polymorphism in simple terms for GCSE?
Polymorphism means "many forms". In programming, it means you can call the same method on objects of different classes and each object responds in its own way. For example, calling .speak() on a Dog gets "Woof" and on a Cat gets "Meow" — the same instruction, different behaviours. It makes code cleaner and easier to extend because new classes can be added without changing existing code.
Does polymorphism appear in the GCSE exam?
OCR GCSE Computer Science (J277) explicitly includes polymorphism as part of the object-oriented programming section. AQA focuses more on classes, objects, inheritance, and encapsulation, but understanding polymorphism strengthens your understanding of all of these. Exam questions might ask you to trace through code with method overriding, predict the output of a loop over mixed-type objects, or explain what polymorphism is in 2–3 sentences.
How is Python polymorphism different from other languages?
In statically-typed languages like Java or C++, polymorphism requires formal class hierarchies and declared types. Python is dynamically typed — it uses "duck typing" (if an object has a speak() method, you can call speak() on it regardless of its class). This makes polymorphism in Python more flexible but also less strictly enforced than in typed languages. At GCSE level, the concept is the same regardless of which language is used.
What does super() do in a subclass?
super() gives access to the parent class from inside a child class. It is most often used in __init__ to call the parent's constructor before adding child-specific attributes:
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # Call Animal's __init__
self.breed = breed
Using super() lets the child class extend the parent's behaviour rather than replace it entirely. It is a complement to method overriding, not an alternative.
For Socratic GCSE Computer Science tutoring on object-oriented programming, inheritance, and Python, visit aitutors.me.