Bird
Raised Fist0
Pythonprogramming~15 mins

Parent and child classes in Python - Deep Dive

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
Overview - Parent and child classes
What is it?
Parent and child classes are a way to organize code where one class (the child) inherits properties and behaviors from another class (the parent). This means the child class can use or change what the parent class has without rewriting everything. It helps to create a clear relationship between general and specific things in your code. This idea is called inheritance.
Why it matters
Without parent and child classes, programmers would have to repeat the same code many times for similar things, making programs longer and harder to fix. Inheritance lets us reuse code easily and build complex programs step-by-step. It also helps keep code organized and easier to understand, which saves time and reduces mistakes.
Where it fits
Before learning parent and child classes, you should understand what classes and objects are in Python. After this, you can learn about more advanced topics like method overriding, multiple inheritance, and design patterns that use inheritance.
Mental Model
Core Idea
A child class is like a specialized version of a parent class that inherits its features but can add or change them.
Think of it like...
Think of a parent class as a blueprint for a general vehicle, and a child class as a blueprint for a specific car model that uses the vehicle blueprint but adds its own features like color or engine type.
Parent Class (Vehicle)
├── Attributes: wheels, color
├── Methods: start(), stop()
└── Child Class (Car)
    ├── Inherits: wheels, color, start(), stop()
    ├── Adds: sunroof, play_music()
    └── Overrides: start() with special behavior
Build-Up - 7 Steps
1
FoundationUnderstanding basic classes
🤔
Concept: Learn what a class is and how to create one in Python.
A class is like a blueprint for creating objects. It defines attributes (data) and methods (actions). For example: class Animal: def __init__(self, name): self.name = name def speak(self): print(f"{self.name} makes a sound")
Result
You can create an Animal object and call its speak method: >>> a = Animal('Dog') >>> a.speak() Dog makes a sound
Understanding classes is the foundation for inheritance because child classes build on this blueprint concept.
2
FoundationCreating objects from classes
🤔
Concept: Learn how to make objects (instances) from a class and use their attributes and methods.
Objects are created by calling the class like a function. Each object has its own data: cat = Animal('Cat') dog = Animal('Dog') cat.speak() # Cat makes a sound dog.speak() # Dog makes a sound
Result
Each object remembers its own name and can perform actions defined by the class.
Knowing how objects work helps you see how child classes create specialized objects with inherited features.
3
IntermediateIntroducing inheritance with child classes
🤔Before reading on: do you think a child class can use methods from its parent class without rewriting them? Commit to your answer.
Concept: Child classes inherit attributes and methods from parent classes, so they can reuse code easily.
To create a child class, put the parent class name in parentheses: class Dog(Animal): pass Now Dog has all Animal's features: d = Dog('Buddy') d.speak() # Buddy makes a sound
Result
The child class Dog can use the speak method from Animal without any new code.
Understanding inheritance shows how child classes save time by reusing parent code instead of copying it.
4
IntermediateAdding new features in child classes
🤔Before reading on: do you think child classes can have their own new methods or attributes besides what they inherit? Commit to your answer.
Concept: Child classes can add new methods or attributes that the parent class does not have.
You can add new features in the child class: class Dog(Animal): def bark(self): print(f"{self.name} barks") my_dog = Dog('Rex') my_dog.speak() # Rex makes a sound my_dog.bark() # Rex barks
Result
The Dog object can both speak (inherited) and bark (new).
Knowing that child classes can extend parents helps you create more specific and useful objects.
5
IntermediateOverriding parent methods in child classes
🤔Before reading on: do you think a child class can change how a parent method works by writing its own version? Commit to your answer.
Concept: Child classes can replace parent methods with their own versions, called overriding.
Override a method by defining it again in the child class: class Dog(Animal): def speak(self): print(f"{self.name} says Woof!") my_dog = Dog('Max') my_dog.speak() # Max says Woof!
Result
The Dog's speak method replaces the Animal's speak method for Dog objects.
Overriding lets child classes customize behavior while still keeping the parent-child relationship.
6
AdvancedUsing super() to call parent methods
🤔Before reading on: do you think a child method can still use the parent method it overrides? Commit to your answer.
Concept: The super() function lets child classes call the parent class's method inside an overridden method.
Use super() to extend parent behavior: class Dog(Animal): def speak(self): super().speak() # call parent method print(f"{self.name} barks loudly") my_dog = Dog('Rocky') my_dog.speak() # Output: # Rocky makes a sound # Rocky barks loudly
Result
The Dog's speak method runs the Animal's speak first, then adds more behavior.
Knowing super() helps combine parent and child behaviors cleanly without repeating code.
7
ExpertUnderstanding method resolution order (MRO)
🤔Before reading on: if a class inherits from multiple parents, which parent's method runs first? Commit to your answer.
Concept: Python uses a specific order called MRO to decide which method to run when multiple parents have the same method.
When a class inherits from multiple parents, Python checks methods in this order: 1. The child class itself 2. The first parent class 3. The next parent class, and so on Example: class A: def greet(self): print('Hello from A') class B: def greet(self): print('Hello from B') class C(A, B): pass c = C() c.greet() # Hello from A Python uses C's MRO: C -> A -> B
Result
The greet method from class A runs because it appears first in the MRO.
Understanding MRO prevents confusion and bugs in complex inheritance setups with multiple parents.
Under the Hood
When Python creates a child class, it builds a method resolution order (MRO) list that tells it where to look for attributes and methods. When you call a method on an object, Python searches the child class first, then its parents in order, until it finds the method. This lookup happens at runtime, allowing dynamic behavior. The super() function uses this MRO to find the next method to call in the chain.
Why designed this way?
Inheritance was designed to promote code reuse and logical organization. The MRO system was created to handle multiple inheritance cleanly and predictably, avoiding ambiguity about which parent method to use. This design balances flexibility with clarity, making Python powerful yet understandable.
┌─────────────┐
│ Child Class │
├─────────────┤
│  Methods    │
│  Attributes │
└─────┬───────┘
      │
      ▼
┌─────────────┐
│ Parent Class│
├─────────────┤
│  Methods    │
│  Attributes │
└─────┬───────┘
      │
      ▼
    (Object)

Method call flow:
Child Class -> Parent Class -> Object
Myth Busters - 4 Common Misconceptions
Quick: Does a child class copy all parent code into itself when created? Commit to yes or no.
Common Belief:A child class copies all the code from the parent class into itself.
Tap to reveal reality
Reality:A child class does not copy code; it references the parent class's methods and attributes dynamically when needed.
Why it matters:Thinking of inheritance as copying can lead to confusion about how changes in parent classes affect children and cause bugs when modifying code.
Quick: Can a child class use a parent method even if it defines a method with the same name? Commit to yes or no.
Common Belief:If a child class defines a method with the same name as the parent, the parent method is lost and cannot be used.
Tap to reveal reality
Reality:The child method overrides the parent method, but the parent method can still be accessed using super().
Why it matters:Not knowing about super() can cause duplicated code or loss of important parent behavior.
Quick: Does Python allow a class to inherit from multiple parents? Commit to yes or no.
Common Belief:Python does not support multiple inheritance; a class can only have one parent.
Tap to reveal reality
Reality:Python supports multiple inheritance, allowing a class to inherit from multiple parents, with a clear method resolution order.
Why it matters:Ignoring multiple inheritance limits design options and understanding of complex class relationships.
Quick: If a parent class changes after child classes are created, do child classes keep the old behavior? Commit to yes or no.
Common Belief:Child classes keep their original behavior even if the parent class changes later.
Tap to reveal reality
Reality:Child classes reflect changes in the parent class immediately because they reference parent methods dynamically.
Why it matters:Misunderstanding this can cause unexpected bugs when modifying parent classes, thinking children are unaffected.
Expert Zone
1
The use of super() is not just a shortcut; it follows the MRO and allows cooperative multiple inheritance, enabling all parents to contribute to method calls.
2
Overriding __init__ in child classes requires careful calling of super().__init__ to ensure proper initialization of parent parts, which is often missed.
3
Python's MRO uses the C3 linearization algorithm, which guarantees a consistent and monotonic order even in complex multiple inheritance hierarchies.
When NOT to use
Inheritance is not ideal when classes do not share a clear 'is-a' relationship or when behavior sharing is better done by composition (has-a relationship). In such cases, prefer composition or delegation patterns to avoid fragile and hard-to-maintain code.
Production Patterns
In real-world systems, inheritance is used to create base classes with common functionality (like database models or UI components) and specialized child classes for specific cases. Frameworks often rely on inheritance for extensibility, but careful use of super() and understanding MRO is critical to avoid bugs.
Connections
Composition over Inheritance
Alternative design pattern
Knowing when to use composition instead of inheritance helps create more flexible and maintainable code by favoring 'has-a' relationships over 'is-a'.
Polymorphism
Builds on inheritance
Inheritance enables polymorphism, where different child classes can be treated as their parent type but behave differently, allowing flexible and reusable code.
Biological Taxonomy
Hierarchical classification analogy
Understanding biological classification (kingdom, genus, species) helps grasp how inheritance organizes classes from general to specific.
Common Pitfalls
#1Forgetting to call the parent __init__ in a child class causes missing initialization.
Wrong approach:class Dog(Animal): def __init__(self, name, breed): self.breed = breed # Missing call to Animal.__init__ my_dog = Dog('Buddy', 'Beagle') print(my_dog.name) # Error: 'Dog' object has no attribute 'name'
Correct approach:class Dog(Animal): def __init__(self, name, breed): super().__init__(name) # Properly initialize parent self.breed = breed my_dog = Dog('Buddy', 'Beagle') print(my_dog.name) # Buddy
Root cause:Misunderstanding that child __init__ replaces parent __init__ unless explicitly called.
#2Overriding a method without calling super() loses parent behavior unintentionally.
Wrong approach:class Dog(Animal): def speak(self): print(f"{self.name} barks") # No call to parent speak my_dog = Dog('Max') my_dog.speak() # Only barks, no parent message
Correct approach:class Dog(Animal): def speak(self): super().speak() # Call parent method print(f"{self.name} barks") my_dog = Dog('Max') my_dog.speak() # Parent and child messages
Root cause:Not realizing that overriding replaces the parent method entirely unless super() is used.
#3Assuming multiple inheritance order is random causes unexpected method calls.
Wrong approach:class A: def greet(self): print('A') class B: def greet(self): print('B') class C(B, A): pass c = C() c.greet() # Prints 'B' but learner expected 'A'
Correct approach:class C(A, B): # Order matters pass c = C() c.greet() # Prints 'A' as expected
Root cause:Ignoring the method resolution order and the order of parent classes in the class definition.
Key Takeaways
Parent and child classes let you organize code by sharing common features and customizing specifics through inheritance.
Child classes inherit methods and attributes from parents but can add new ones or override existing ones to change behavior.
The super() function is essential for combining parent and child behaviors, especially in complex inheritance chains.
Python uses a method resolution order (MRO) to decide which method to run when multiple parents define the same method.
Understanding when to use inheritance versus composition is key to writing clear, maintainable, and flexible code.

Practice

(1/5)
1. What is the main purpose of a parent class in Python?
easy
A. To hold common features that child classes can inherit
B. To override methods in child classes
C. To create instances directly without child classes
D. To prevent child classes from adding new features

Solution

  1. Step 1: Understand the role of parent class

    A parent class is designed to hold common features like methods and attributes that multiple child classes can share.
  2. Step 2: Compare options with this role

    To hold common features that child classes can inherit correctly states this purpose. Other options describe incorrect or unrelated roles.
  3. Final Answer:

    To hold common features that child classes can inherit -> Option A
  4. Quick Check:

    Parent class = common features [OK]
Hint: Parent class shares features for children to reuse [OK]
Common Mistakes:
  • Thinking parent classes prevent changes in children
  • Believing parent classes are only for direct instances
  • Confusing overriding with inheritance
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 extends 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 from a parent class by placing the parent class name in parentheses after the child class name.
  2. Step 2: Match options with correct syntax

    class Dog(Animal): uses class Dog(Animal): which is correct. Other options use incorrect keywords or symbols not valid in Python.
  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' or 'extends' keywords (not Python)
  • Using symbols like '<' instead of parentheses
  • Omitting the colon at the end
3. What will be the output of this code?
class Parent:
    def greet(self):
        return "Hello from Parent"

class Child(Parent):
    def greet(self):
        return "Hello from Child"

obj = Child()
print(obj.greet())
medium
A. Hello from ParentChild
B. Hello from Parent
C. Hello from Child
D. Error: greet method not found

Solution

  1. Step 1: Understand method overriding in child class

    The child class Child defines its own greet method, which replaces the parent's method when called on a child instance.
  2. Step 2: Check which method is called

    Since obj is an instance of Child, calling obj.greet() uses the child's method, returning "Hello from Child".
  3. Final Answer:

    Hello from Child -> Option C
  4. Quick Check:

    Child method overrides parent method = Hello from Child [OK]
Hint: Child method overrides parent method when called on child [OK]
Common Mistakes:
  • Thinking parent method runs instead of child's
  • Expecting combined output from both methods
  • Assuming method not found error
4. Find the error in this code:
class Parent:
    def __init__(self, name):
        self.name = name

class Child(Parent):
    def __init__(self, name, age):
        self.age = age

c = Child('Anna', 10)
print(c.name, c.age)
medium
A. Child class __init__ does not call Parent __init__, so name is missing
B. Syntax error in class definition
C. Cannot create Child instance with two arguments
D. print statement syntax is wrong

Solution

  1. Step 1: Check constructor chaining in child class

    The child class __init__ method sets age but does not call super().__init__(name) to set name from the parent.
  2. Step 2: Understand consequence of missing super call

    Because name is not set in Child, accessing c.name will cause an error or missing attribute.
  3. Final Answer:

    Child class __init__ does not call Parent __init__, so name is missing -> Option A
  4. Quick Check:

    Missing super() call = missing parent attributes [OK]
Hint: Always call super().__init__ in child __init__ to set parent attributes [OK]
Common Mistakes:
  • Forgetting to call super().__init__ in child constructor
  • Assuming parent attributes set automatically
  • Confusing syntax errors with logic errors
5. Given these classes, what will print(c.describe()) output?
class Parent:
    def describe(self):
        return "I am a parent"

class Child(Parent):
    def describe(self):
        parent_desc = super().describe()
        return parent_desc + " and I am a child"

c = Child()
hard
A. I am a parent
B. I am a parent and I am a child
C. I am a child
D. Error: super() used incorrectly

Solution

  1. Step 1: Understand super() usage in child describe method

    The child method calls super().describe() which runs the parent method returning "I am a parent".
  2. Step 2: Combine parent and child strings

    The child method adds " and I am a child" to the parent's string, so the full return is "I am a parent and I am a child".
  3. Final Answer:

    I am a parent and I am a child -> Option B
  4. Quick Check:

    super() calls parent method, combined output [OK]
Hint: Use super() to add parent behavior inside child method [OK]
Common Mistakes:
  • Expecting only child or only parent output
  • Thinking super() causes error without arguments
  • Ignoring string concatenation