Courseiva
Object-Oriented Programming →mediumMultiple Choice

Python Method Overriding and Dynamic Dispatch

A Python class 'BankAccount' has a method 'withdraw(amount)' that deducts 'amount' from 'self.balance'. A developer writes a subclass 'SavingsAccount' that overrides 'withdraw' to add a penalty if balance drops below minimum. Which design pattern is being used?

Quick Answer

The correct answer is method overriding, because the SavingsAccount subclass redefines the withdraw method from BankAccount to add penalty logic while preserving the same method signature. This is the defining characteristic of method overriding in Python: a subclass provides its own specific implementation of a method already defined in its superclass, allowing dynamic dispatch at runtime to call the appropriate version based on the object’s actual type. On the Certified Associate Python Programmer PCAP exam, this concept tests your understanding of inheritance and polymorphism, often appearing in questions that ask you to identify the design pattern when a subclass alters a parent method’s behavior. A common trap is confusing overriding with overloading—remember that Python does not support true method overloading; overriding always involves the same method name and parameters. Memory tip: think “override = replace the parent’s version” and “overload = same name, different arguments (not Python’s style).”

⚠ Common exam trap

Many candidates confuse inheritance (the relationship) with method overriding (the specific technique), leading them to select 'Inheritance' instead of 'Method overriding' when the question explicitly describes a subclass redefining a parent method.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

Method overriding

Method overriding is the mechanism where a subclass provides a specific implementation of a method that is already defined in its superclass. In this scenario, SavingsAccount overrides the withdraw method from BankAccount to add penalty logic, which is the defining characteristic of method overriding in Python.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Composition

    Why it's wrong here

    Composition builds behaviour by holding references to other objects and delegating calls to them, whereas SavingsAccount extends BankAccount and overrides withdraw directly. No contained object is involved here. Composition would be correct if a class assembled behaviour from separate collaborator instances rather than subclassing.

  • ✗

    Aggregation

    Why it's wrong here

    Aggregation is a whole-part association where objects are collected but remain independently owned, such as a Team containing Player objects. SavingsAccount neither contains nor references BankAccount instances; it derives from it. Aggregation would apply if behaviour were assembled from separate account objects at runtime.

  • ✓

    Method overriding

    Why this is correct

    SavingsAccount redefines the inherited withdraw method with its own penalty logic while keeping the same signature, so Python dispatches to the subclass version at runtime. That substitution of a superclass method implementation is method overriding, not overloading or composition.

  • ✗

    Inheritance

    Why it's wrong here

    Inheritance is the mechanism itself, not the design pattern being asked about; the question targets the pattern governing the override relationship. Subclassing with overridden behaviour exemplifies the Template Method pattern, where a superclass defines the algorithm skeleton and subclasses redefine steps. Inheritance alone describes the language feature.

About these practice questions

This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on PCAP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. An engineer is debugging an application that uses inheritance. The base class 'Vehicle' defines a method 'start()' that prints 'Vehicle started'. The subclass 'Car' overrides 'start()' to print 'Car started'. The code contains a function that accepts a Vehicle object and calls 'start()'. What is the output if a Car object is passed?

medium
  • A.'Car started Vehicle started'
  • B.'Vehicle started'
  • ✓ C.'Car started'
  • D.AttributeError

Why C: When a Car object is passed to a function expecting a Vehicle reference, Python uses dynamic dispatch (late binding) to call the overridden `start()` method defined in the Car class. Since the actual runtime type is Car, the overridden version executes, printing 'Car started'. This is a fundamental principle of polymorphism in Python.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.