Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Compare Overridden and Inherited Behavior in a Class Hierarchy

Determine whether a derived class method is overridden or inherited and compare runtime results.

Python practice25 minInheritance & PolymorphismAdvancedLast updated April 8, 2026

Problem statement

Given two classes (a base class and a derived class) and the name of a method that exists on the base class, write a function compare_methods(base_cls, derived_cls, method_name, *args, **kwargs) that: - Instantiates base_cls and derived_cls with no constructor arguments. - Calls the method named method_name on both instances with the provided arguments. - Returns a tuple (base_result, derived_result, status) where status is the string 'overridden' if derived_cls defines its own implementation of method_name (i.e., the method is present in derived_cls.__dict__), or 'inherited' if the derived class does not define the method (it comes from its parent or further up the MRO). Your implementation must handle typical instance methods. You can assume the method exists on the base class and that both classes can be instantiated without arguments.

Task

Implement a utility that instantiates a base and a derived class, invokes a named method on each, and reports the outputs plus whether the derived class overrides that method.

Examples

Derived overrides a method

Input

compare_methods(Animal, Dog, 'speak')

Output

('...', 'woof', 'overridden')

Animal.speak returns '...'; Dog overrides speak to return 'woof'. The function reports both outputs and 'overridden'.

Input format

An expression calling compare_methods with a base class, a derived class, the method name string, and optional positional/keyword args for the method.

Output format

A tuple (base_result, derived_result, status) where status is 'overridden' or 'inherited'. The test harness compares the string form of this tuple.

Constraints

Both classes can be instantiated without arguments. The method_name exists on the base class. You should detect override by checking derived_cls.__dict__ for the method name.

Samples

Sample 1

Input

compare_methods(BaseMath, MultChild, 'compute', 2, 3)

Output

(5, 6, 'overridden')

BaseMath.compute returns 2+3=5; MultChild overrides compute to multiply and returns 6.