Bird
Raised Fist0
Pythonprogramming~30 mins

Extending parent behavior in Python - Mini Project: Build & Apply

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Extending Parent Behavior
📖 Scenario: Imagine you are creating a simple system to manage different types of employees in a company. Each employee has a name and a method to describe their work. You want to create a special type of employee called a Manager who does everything a normal employee does but also manages a team.
🎯 Goal: You will build two classes: Employee and Manager. The Manager class will extend the Employee class and add extra behavior by extending the parent method.
📋 What You'll Learn
Create a class called Employee with an __init__ method that takes name as a parameter.
Add a method called work in Employee that returns the string "Employee {name} is working".
Create a class called Manager that inherits from Employee.
Override the work method in Manager to extend the behavior of Employee.work by adding " and managing the team" to the returned string.
Create an instance of Manager with the name "Alice" and print the result of calling its work method.
💡 Why This Matters
🌍 Real World
Extending parent behavior is common when you want to add or change features in a new type of object without rewriting existing code. For example, managers have all employee features plus extra responsibilities.
💼 Career
Understanding inheritance and method overriding is essential for writing clean, reusable code in many programming jobs, especially in software development and object-oriented design.
Progress0 / 4 steps
1
Create the Employee class
Create a class called Employee with an __init__ method that takes a parameter name and stores it in self.name. Also, add a method called work that returns the string f"Employee {self.name} is working".
Python
Hint

Remember to use self.name = name inside the __init__ method to save the name.

2
Create the Manager class inheriting Employee
Create a class called Manager that inherits from Employee. Do not add any methods yet.
Python
Hint

Use parentheses to inherit: class Manager(Employee):

3
Override the work method in Manager
In the Manager class, override the work method. Inside it, call the parent class work method using super().work() and add the string " and managing the team" to the result. Return the combined string.
Python
Hint

Use super().work() to get the parent method's result.

4
Create Manager instance and print work result
Create an instance of Manager called manager with the name "Alice". Then print the result of calling manager.work().
Python
Hint

Create the instance with Manager("Alice") and print manager.work().

Practice

(1/5)
1. What does super() do in a child class method?
easy
A. It overrides the child class method completely.
B. It calls the parent class method to reuse its behavior.
C. It deletes the parent class method.
D. It creates a new instance of the child class.

Solution

  1. Step 1: Understand the role of super()

    super() is used to call a method from the parent class inside a child class method.
  2. Step 2: Recognize code reuse

    By calling the parent method, the child can reuse existing behavior and add new features without rewriting code.
  3. Final Answer:

    It calls the parent class method to reuse its behavior. -> Option B
  4. Quick Check:

    super() calls parent method = D [OK]
Hint: Remember: super() runs parent method inside child [OK]
Common Mistakes:
  • Thinking super() creates new instances
  • Believing super() deletes methods
  • Assuming super() overrides without calling parent
2. Which of the following is the correct syntax to call a parent class method greet inside a child class method in Python?
easy
A. super().greet()
B. super.greet()
C. parent.greet()
D. self.super.greet()

Solution

  1. Step 1: Recall correct super() syntax

    In Python, super() is a function and must be called with parentheses before accessing methods.
  2. Step 2: Identify correct method call

    The correct way to call the parent method is super().greet(), not super.greet() or others.
  3. Final Answer:

    super().greet() -> Option A
  4. Quick Check:

    super() needs parentheses = A [OK]
Hint: Use parentheses with super() to call parent methods [OK]
Common Mistakes:
  • Omitting parentheses after super
  • Using parent instead of super
  • Trying to access super as an attribute
3. What will be the output of this code?
class Parent:
    def greet(self):
        print('Hello from Parent')

class Child(Parent):
    def greet(self):
        super().greet()
        print('Hello from Child')

c = Child()
c.greet()
medium
A. Hello from Child
B. Error: super() not defined
C. Hello from Parent
D. Hello from Parent Hello from Child

Solution

  1. Step 1: Trace the child greet() method

    The child method calls super().greet() first, which prints 'Hello from Parent'.
  2. Step 2: Continue child method execution

    After calling the parent method, it prints 'Hello from Child'. So both lines print in order.
  3. Final Answer:

    Hello from Parent Hello from Child -> Option D
  4. Quick Check:

    super() calls parent then child prints = B [OK]
Hint: super() runs parent code first, then child adds more [OK]
Common Mistakes:
  • Ignoring the parent print
  • Expecting only child output
  • Thinking super() causes error
4. Find the error in this code that tries to extend the parent method:
class Parent:
    def show(self):
        print('Parent show')

class Child(Parent):
    def show(self):
        super.show()
        print('Child show')
medium
A. super.show() should be super().show()
B. Child class must not override show()
C. Parent class method show() is missing self
D. print statements must be inside __init__

Solution

  1. Step 1: Check super() usage

    The code uses super.show() which is incorrect syntax; super() must be called as a function.
  2. Step 2: Correct the syntax

    It should be super().show() to properly call the parent method.
  3. Final Answer:

    super.show() should be super().show() -> Option A
  4. Quick Check:

    super() needs parentheses to call methods = A [OK]
Hint: Always use super() with parentheses to call parent methods [OK]
Common Mistakes:
  • Forgetting parentheses after super
  • Thinking parent method needs no self
  • Believing print must be in __init__
5. You want to extend a parent class method calculate so that the child class adds 10 to the parent's result. Which code correctly does this?
class Parent:
    def calculate(self):
        return 5

class Child(Parent):
    def calculate(self):
        # Fill here
hard
A. return Parent.calculate() + 10
B. return calculate() + 10
C. return super().calculate() + 10
D. return self.calculate() + 10

Solution

  1. Step 1: Use super() to call parent method

    To get the parent's result, call super().calculate() inside the child method.
  2. Step 2: Add 10 to the parent's result

    Return the parent's value plus 10 as super().calculate() + 10.
  3. Final Answer:

    return super().calculate() + 10 -> Option C
  4. Quick Check:

    super() calls parent, add 10 = C [OK]
Hint: Use return super().method() + extra to extend result [OK]
Common Mistakes:
  • Calling calculate() without super causes recursion
  • Calling Parent.calculate() without instance
  • Using self.calculate() causes infinite loop