Bird
Raised Fist0
Pythonprogramming~10 mins

Purpose of inheritance in Python - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a class that inherits from another class.

Python
class Animal:
    def sound(self):
        return "Some sound"

class Dog([1]):
    pass
Drag options to blanks, or click blank then click option'
ADog
BAnimal
CSound
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using the child class name inside parentheses instead of the parent class.
Forgetting to put parentheses after the class name.
2fill in blank
medium

Complete the code to call the inherited method from the child class instance.

Python
class Animal:
    def sound(self):
        return "Some sound"

class Cat(Animal):
    pass

pet = Cat()
print(pet.[1]())
Drag options to blanks, or click blank then click option'
Asound
Bcall
Cnoise
Dmake_sound
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist in the parent class.
Trying to call the method without parentheses.
3fill in blank
hard

Fix the error in the code by completing the inheritance syntax correctly.

Python
class Vehicle:
    def move(self):
        return "Moving"

class Car[1]Vehicle):
    pass
Drag options to blanks, or click blank then click option'
A(
B:
C[
D{
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the opening parenthesis before the parent class name.
Using square brackets or braces instead of parentheses.
4fill in blank
hard

Fill both blanks to create a child class that overrides a method from the parent class.

Python
class Bird:
    def fly(self):
        return "Flying"

class Penguin([1]):
    def fly(self):
        return [2]
Drag options to blanks, or click blank then click option'
ABird
B"Cannot fly"
C"Flying"
DAnimal
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parent class name.
Not returning a string in the overridden method.
5fill in blank
hard

Fill all three blanks to create a class hierarchy and call a method from the grandparent class.

Python
class LivingThing:
    def breathe(self):
        return "Breathing"

class Animal([1]):
    pass

class Dog([2]):
    def breathe(self):
        return super().[3]()
Drag options to blanks, or click blank then click option'
ALivingThing
BAnimal
Cbreathe
DDog
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong class names in inheritance.
Forgetting to call the method with parentheses.

Practice

(1/5)
1. What is the main purpose of inheritance in Python?
easy
A. To delete objects from memory
B. To allow a class to reuse code from another class
C. To make a program run faster
D. To create variables inside a function

Solution

  1. Step 1: Understand inheritance concept

    Inheritance lets one class (child) use code from another class (parent).
  2. Step 2: Identify main benefit

    This helps reuse code and avoid rewriting the same features.
  3. Final Answer:

    To allow a class to reuse code from another class -> Option B
  4. Quick Check:

    Inheritance = Code reuse [OK]
Hint: Inheritance means reusing code from another class [OK]
Common Mistakes:
  • Confusing inheritance with variable creation
  • Thinking inheritance speeds up program execution
  • Believing inheritance deletes objects
2. Which of the following is the correct syntax to define a child class Dog that inherits from a parent class Animal?
easy
A. class Dog : Animal
B. class Dog inherits Animal:
C. class Dog -> Animal:
D. class Dog(Animal):

Solution

  1. Step 1: Recall Python inheritance syntax

    In Python, a child class inherits by putting the parent class name in parentheses after the child class name.
  2. Step 2: Match syntax options

    Only class Dog(Animal): follows this rule correctly.
  3. Final Answer:

    class Dog(Animal): -> Option D
  4. Quick Check:

    Child class syntax = class Child(Parent): [OK]
Hint: Use parentheses with parent class name after child class [OK]
Common Mistakes:
  • Using 'inherits' keyword which doesn't exist in Python
  • Using arrow '->' instead of parentheses
  • Using colon ':' incorrectly after class name
3. What will be the output of this code?
class Animal:
    def sound(self):
        return "Some sound"

class Dog(Animal):
    def sound(self):
        return "Bark"

pet = Dog()
print(pet.sound())
medium
A. Some sound
B. None
C. Bark
D. Error

Solution

  1. Step 1: Understand method overriding in inheritance

    The Dog class has its own sound method that replaces the one from Animal.
  2. Step 2: Check which method is called

    When pet.sound() runs, it uses the Dog version returning "Bark".
  3. Final Answer:

    Bark -> Option C
  4. Quick Check:

    Child method overrides parent method = Bark [OK]
Hint: Child method replaces parent method if same name used [OK]
Common Mistakes:
  • Expecting parent method output instead of child
  • Thinking both methods run together
  • Assuming error due to method name clash
4. Find the error in this inheritance code:
class Vehicle:
    def move(self):
        print("Moving")

class Car(Vehicle)
    def move(self):
        print("Car moving")
medium
A. Missing colon after class Car(Vehicle)
B. Wrong parent class name
C. Indentation error in move method
D. No error

Solution

  1. Step 1: Check class definition syntax

    Python requires a colon ':' at the end of class header lines.
  2. Step 2: Identify missing colon

    The line class Car(Vehicle) misses the colon at the end.
  3. Final Answer:

    Missing colon after class Car(Vehicle) -> Option A
  4. Quick Check:

    Class header must end with ':' [OK]
Hint: Always put ':' after class and function headers [OK]
Common Mistakes:
  • Forgetting colon after class definition
  • Assuming wrong parent class name causes error
  • Confusing indentation errors with syntax errors
5. You want to create a class SmartPhone that inherits features from both Phone and Camera classes. What is the correct way to define SmartPhone to reuse code from both parents?
hard
A. class SmartPhone(Phone, Camera):
B. class SmartPhone inherits Phone and Camera:
C. class SmartPhone(Phone): Camera
D. class SmartPhone : Phone, Camera

Solution

  1. Step 1: Understand multiple inheritance syntax

    Python allows a class to inherit from multiple parents by listing them in parentheses separated by commas.
  2. Step 2: Match correct syntax

    Only class SmartPhone(Phone, Camera): correctly shows multiple inheritance.
  3. Final Answer:

    class SmartPhone(Phone, Camera): -> Option A
  4. Quick Check:

    Multiple inheritance uses commas inside parentheses [OK]
Hint: List multiple parents separated by commas in parentheses [OK]
Common Mistakes:
  • Using 'inherits' keyword which is invalid
  • Trying to separate parents with colon or outside parentheses
  • Forgetting parentheses around parent classes