Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Dispatch Actions Based on Subclass Type Polymorphically

Implement subclass-specific behavior and call it through a common interface without type checks.

Python practice15 minInheritance & PolymorphismIntermediateLast updated April 8, 2026

Problem statement

You are given a base class Worker with several subclasses (Programmer, Chef, Teacher). Each subclass should implement its own work() method that returns a string describing the action the worker performs. Implement the subclasses' work methods so that a single function do_work(worker) can call the correct behavior polymorphically (without using isinstance or type checks).

Task

Learn to override methods in subclasses and use a single dispatcher function that relies on polymorphism rather than explicit type checks.

Examples

Programmer example

Input

do_work(Programmer('Ada'))

Output

Programmer Ada is coding

Programmer overrides work() to return a message about coding; do_work simply calls worker.work() and returns that string.

Input format

A single Worker subclass instance passed to do_work.

Output format

A string describing what the worker is doing.

Constraints

Do not use isinstance, type checks, or conditional branching based on subclass type. Rely on method overriding (polymorphism).

Samples

Sample 1

Input

do_work(Chef('Gordon'))

Output

Chef Gordon is cooking

Chef.work() returns the cooking message and do_work returns it unchanged.