Bird
Raised Fist0
Pythonprogramming~10 mins

Inheriting attributes and methods 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 make class Dog inherit from class Animal.

Python
class Animal:
    def __init__(self, name):
        self.name = name

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

Complete the code to call the parent class constructor inside the child class.

Python
class Animal:
    def __init__(self, name):
        self.name = name

class Dog(Animal):
    def __init__(self, name, breed):
        [1](name)
        self.breed = breed
Drag options to blanks, or click blank then click option'
Asuper().__init__
BAnimal.__init__
CDog.__init__
Dself.__init__
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.__init__ which causes infinite recursion.
Calling the child class constructor instead of the parent.
3fill in blank
hard

Fix the error in the code to correctly inherit and use the method from the parent class.

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

class Cat(Animal):
    def speak(self):
        return [1]
Drag options to blanks, or click blank then click option'
Aself.speak() + " meow"
BAnimal.speak() + " meow"
Csuper().speak() + " meow"
D"meow"
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.speak() inside speak causes infinite recursion.
Calling the parent method without super() and missing the instance.
4fill in blank
hard

Fill both blanks to create a child class that inherits and extends the parent class method.

Python
class Vehicle:
    def description(self):
        return "Vehicle"

class Car([1]):
    def description(self):
        return super().[2]() + " - Car"
Drag options to blanks, or click blank then click option'
AVehicle
Bdescription
Cdesc
DCar
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parent class name.
Calling a method name that does not exist in the parent.
5fill in blank
hard

Fill all three blanks to create a child class that inherits attributes and overrides a method using the parent method.

Python
class Person:
    def __init__(self, name):
        self.name = name
    def greet(self):
        return f"Hello, I am {self.name}"

class Student([1]):
    def __init__(self, name, student_id):
        [2](name)
        self.student_id = student_id
    def greet(self):
        return [3]() + f", my student ID is {self.student_id}"
Drag options to blanks, or click blank then click option'
APerson
Bsuper().__init__
Csuper().greet
Dself.__init__
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.__init__ instead of super().__init__.
Not calling the parent constructor at all.
Not using super() to call the parent method in greet.

Practice

(1/5)
1. What does it mean when a child class inherits from a parent class in Python?
easy
A. The child class must redefine all methods of the parent class to use them.
B. The child class can only use methods but not attributes of the parent class.
C. The child class automatically has all attributes and methods of the parent class.
D. The child class cannot add any new methods or attributes.

Solution

  1. Step 1: Understand inheritance basics

    Inheritance means the child class gets all features (attributes and methods) of the parent class automatically.
  2. Step 2: Analyze each option

    The child class automatically has all attributes and methods of the parent class. correctly states this. Options B, C, and D are incorrect because they limit or deny inheritance features.
  3. Final Answer:

    The child class automatically has all attributes and methods of the parent class. -> Option C
  4. Quick Check:

    Inheritance = automatic access to parent features [OK]
Hint: Inheritance means child gets all parent features automatically [OK]
Common Mistakes:
  • Thinking child must redefine parent methods
  • Believing child cannot add new features
  • Assuming attributes are not inherited
2. Which of the following is the correct syntax to make class Dog inherit from class Animal in Python?
easy
A. class Dog(Animal):
B. class Dog -> Animal:
C. class Dog inherits Animal:
D. class Dog : Animal

Solution

  1. Step 1: Recall Python inheritance syntax

    In Python, inheritance is shown by putting the parent class name in parentheses after the child class name.
  2. Step 2: Match syntax to options

    class Dog(Animal): uses class Dog(Animal): which is correct. Others use invalid syntax.
  3. Final Answer:

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

    Inheritance syntax = class Child(Parent): [OK]
Hint: Use parentheses with parent class name after child class [OK]
Common Mistakes:
  • Using 'inherits' keyword instead of parentheses
  • Using arrow or colon incorrectly
  • Omitting parentheses
3. What will be the output of this code?
class Parent:
    def greet(self):
        return "Hello from Parent"

class Child(Parent):
    pass

c = Child()
print(c.greet())
medium
A. Hello from Child
B. Hello from Parent
C. AttributeError
D. SyntaxError

Solution

  1. Step 1: Understand method inheritance

    Child class inherits greet method from Parent because it has no own greet.
  2. Step 2: Trace the method call

    Calling c.greet() runs Parent's greet returning "Hello from Parent".
  3. Final Answer:

    Hello from Parent -> Option B
  4. Quick Check:

    Inherited method runs if child has none [OK]
Hint: Child uses parent's method if not overridden [OK]
Common Mistakes:
  • Expecting child's own greet method when none exists
  • Confusing AttributeError with missing method
  • Thinking syntax error occurs
4. Find the error in this code:
class Animal:
    def sound(self):
        return "Some sound"

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

d = Dog()
print(d.sound())
medium
A. print statement syntax error
B. Dog class should not inherit Animal
C. Cannot override methods in child class
D. Missing self parameter in Dog's sound method

Solution

  1. Step 1: Check method definitions

    In Python, instance methods must have self as first parameter.
  2. Step 2: Identify error in Dog's sound

    Dog's sound method lacks self, causing a TypeError when called on instance.
  3. Final Answer:

    Missing self parameter in Dog's sound method -> Option D
  4. Quick Check:

    Instance methods need self parameter [OK]
Hint: Always include self as first method parameter [OK]
Common Mistakes:
  • Forgetting self in child method
  • Thinking inheritance disallows overriding
  • Assuming print syntax is wrong
5. Given these classes:
class Vehicle:
    def __init__(self, brand):
        self.brand = brand
    def info(self):
        return f"Vehicle brand: {self.brand}"

class Car(Vehicle):
    def __init__(self, brand, model):
        super().__init__(brand)
        self.model = model
    def info(self):
        return f"Car brand: {self.brand}, model: {self.model}"

What will print(Car('Toyota', 'Corolla').info()) output?
hard
A. Car brand: Toyota, model: Corolla
B. Vehicle brand: Toyota
C. Car brand: , model: Corolla
D. TypeError due to missing argument

Solution

  1. Step 1: Understand constructor chaining

    Car's __init__ calls super().__init__(brand) to set brand in Vehicle.
  2. Step 2: Analyze info method override

    Car overrides info to include both brand and model.
  3. Step 3: Predict output

    Calling info() on Car instance returns "Car brand: Toyota, model: Corolla".
  4. Final Answer:

    Car brand: Toyota, model: Corolla -> Option A
  5. Quick Check:

    Child overrides method and calls parent's init [OK]
Hint: Use super() to inherit parent init, override methods as needed [OK]
Common Mistakes:
  • Forgetting super() call in child __init__
  • Expecting parent info output instead of child's
  • Confusing missing arguments error