Creating Child Classes

Build child classes that add their own attributes and methods on top of what they inherit.

Introduction

In the previous lesson, the child class just used pass and inherited everything as-is. Now let's create child classes that add their own attributes and methods, making them more specialized than their parent.

Adding New Attributes

A child class can define its own __init__ to accept extra parameters. Use super().__init__() to let the parent set up its attributes first, then add the child's own.

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

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)
        self.breed = breed

d = Dog("Buddy", "Labrador")
print(d.name)   # from parent
print(d.breed)  # from child

Adding New Methods

Child classes can also define entirely new methods that the parent does not have. The child gets its own unique behavior while still keeping everything from the parent.

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)
        self.breed = breed

    def fetch(self):
        print(f"{self.name} fetches the ball!")

d = Dog("Buddy", "Labrador")
d.fetch()  # Buddy fetches the ball!

The Child Has Everything

A child class has access to all parent attributes, all parent methods, plus its own additions. Think of it as a superset of the parent.

Key idea: The parent class does not know about the child's extra features. But the child knows about everything the parent has. Information flows downward in the hierarchy.

Example: Shape to Rectangle

class Shape:
    def __init__(self, color):
        self.color = color

class Rectangle(Shape):
    def __init__(self, color, width, height):
        super().__init__(color)
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

r = Rectangle("blue", 5, 3)
print(r.color)   # blue -- from Shape
print(r.area())  # 15 -- from Rectangle

The Rectangle inherits color from Shape and adds its own width, height, and area() method. Clean and organized.

Real-World Connection

Think of a company hierarchy. All employees share a name and ID (parent). But a developer also has a programming language, and a designer has a tool of choice. Each child class extends the base with role-specific details.

Best Practices

  • Always call super().__init__() in the child's __init__ to properly initialize parent attributes.
  • Keep child-specific attributes in the child class, not the parent.
  • Name methods clearly so it is obvious which class they belong to conceptually.

Common Mistakes

  • Forgetting to call super().__init__() -- the parent's attributes will not be set up.
  • Duplicating parent attributes in the child instead of inheriting them.
  • Passing wrong arguments to super().__init__() -- match what the parent expects.

Practice Exercises

  1. Create a Person class with name and age. Create a Teacher child class that adds a subject attribute and a teach() method.
  2. Create a Shape class with color. Create a Circle class that adds radius and an area() method. Print the color and area.
  3. Create a Vehicle class with brand and year. Create a Truck class that adds payload_capacity and a load() method.

Summary

  • Child classes can add new attributes by defining their own __init__ and calling super().__init__().
  • Child classes can add new methods that the parent does not have.
  • The child has everything the parent has, plus its own additions.
  • Always call super().__init__() to ensure the parent is properly initialized.