Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Call the Same Method on Different Subclass Objects

Practice polymorphism by defining a base class and multiple subclasses that override the same method. Call that method on a list of mixed objects.

Python practice15 minInheritance & PolymorphismIntermediateLast updated April 8, 2026

Problem statement

You will create a simple class hierarchy to demonstrate polymorphism. Define a base class Worker with a method work(). Then implement three subclasses (Chef, Programmer, Driver) that override work() to return strings describing the action they perform. Finally, implement collect_tasks(workers) that accepts a list of Worker instances (or instances of subclasses), calls work() on each, and returns a list of the returned strings in the same order. This exercise emphasizes that the same method name can be called on different subclass instances and produce behavior specific to each subclass.

Task

Design a class hierarchy with a common interface method and write a function that invokes that method on different subclass instances, demonstrating polymorphic behavior.

Examples

Basic usage

Input

collect_tasks([Chef(), Programmer(), Driver()])

Output

['cooking', 'coding', 'driving']

Each subclass implements work() differently. collect_tasks calls work() on each instance and returns the results in list form.

Input format

A single function call: collect_tasks(list_of_workers). The list contains instances of Worker or its subclasses.

Output format

A list of strings representing the result of calling work() on each instance, in order.

Constraints

Each element in the input list will be an instance of Worker or a subclass. The returned list should preserve input order. No external libraries are required.

Samples

Sample 1

Input

collect_tasks([Chef()])

Output

['cooking']

A single Chef returns 'cooking'.