Introduction to Inheritance

Lesson 3 of 7 • 10 XP

Keep your place in this quest

Log in or sign up for free to subscribe, follow lesson progress, and access more learning content.

Inheritance is one of the core concepts of Object-Oriented Programming (OOP).
It allows one class (called the child or subclass) to inherit the attributes and methods of another class (called the parent or superclass).

This means you can define common behavior and data in a base class, then create more specialized classes that reuse and customize that functionality — without having to rewrite the same code.


Basic Syntax for Inheritance

In Python, inheritance is implemented by specifying the parent class name in parentheses after the child class name.

class ParentClass:
    # Attributes and methods of the parent class

class ChildClass(ParentClass):
    # Attributes and methods of the child class
````

When a child class inherits from a parent class, it gains:

* All the parent’s **attributes** (variables tied to the object)
* All the parent’s **methods** (functions tied to the object)
* Any **constructor** (`__init__`) the parent class may have

---

# Example: Animals and Sounds

Let’s bring inheritance to life with something we can all relate to — animals.  
We’ll start with a general `Animal` class that defines what all animals have in common, and then we’ll create specific types of animals (`Dog` and `Cat`) that share those traits but make different sounds.

Here’s the base class:

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

    def sound(self):
        pass  # We'll let each specific animal decide its own sound
````

The `Animal` class has:

* An `__init__` method that sets up the `name` and `age` for each animal.
* A `sound()` method that doesn’t do anything yet (`pass` is just a placeholder).
  We’ll replace it in each animal type with the sound that species makes.

---

Now let’s create two specific animal classes:

```python
class Dog(Animal):
    def sound(self):
        print("Woof woof!")

class Cat(Animal):
    def sound(self):
        print("Meow!")

Here’s what’s happening:

  • Both Dog and Cat inherit from Animal, so they automatically get the name and age attributes without us having to rewrite that code.
  • Each one overrides the sound() method to make its own unique sound.

Creating and Using the Objects

dog = Dog("Rex", 2)
cat = Cat("Mimi", 3)

print(dog.name)  # Rex
dog.sound()      # Woof woof!

print(cat.name)  # Mimi
cat.sound()      # Meow!

When we create Dog("Rex", 2), here’s what happens step-by-step:

  1. Python calls the __init__ method from Animal (because Dog doesn’t have its own).
  2. The name and age values we pass in are stored in that new object.
  3. When we call dog.sound(), Python looks for a sound() method in Dog first. It finds the one we wrote there and runs it — printing "Woof woof!".

The same logic applies to Cat, but it finds and runs its own sound() method.


This example shows the magic of inheritance:

  • We write the common setup (__init__) only once, in the Animal class.
  • Each subclass (Dog, Cat) keeps the shared features and adds its own unique behavior.
  • The result is less repetitive code, more organized logic, and an easier way to manage related objects.

3. How This Works

  • Shared structure: The __init__ method from Animal runs when you create a Dog or Cat object, because they inherit it automatically.
  • Method overriding: Both Dog and Cat have their own sound() methods that replace (override) the placeholder in Animal. When you call sound() on a Dog, Python uses the one defined in Dog, not the one in Animal.
TIP: If a child class doesn’t override a method, it uses the one from the parent class automatically.

Why Use Inheritance?

Inheritance lets you:

  • Avoid repeating code by putting shared logic in a single base class.
  • Make your code more modular and readable.
  • Build specialized classes from a general blueprint.

Example of thinking in inheritance:

  • Animal → general blueprint with name, age, and a sound() method.
  • Dog → a specific type of animal with its own unique sound.
  • Cat → another specific type of animal with a different sound.

Wrapping Up

Inheritance is a powerful tool in OOP for creating a hierarchy of classes, starting from general concepts and moving toward more specialized ones.

In Python, you implement it by:

  1. Writing a parent class with common attributes and methods.
  2. Creating child classes that list the parent class name in parentheses.
  3. Adding or overriding methods in the child classes as needed.

In the next lesson, we’ll build on this concept by exploring super() — a way for child classes to call and extend the behavior of their parent classes.