Python Inheritance

Let’s look at inheritance

  1. Notice

    A subclass can reuse behavior from its parent and add or override methods. Attribute lookup follows the inheritance hierarchy.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
class Animal:
    def speak(self):
        return "sound"
class Dog(Animal):
    def speak(self):
        return 'woof'
pet = Dog()
print(pet.speak())
Live state

Top-level scope

Animal→Object 2
Dog→Object 4
pet→Object 5
Object 2 · typeclass AnimalObject 4 · typeclass DogObject 5 · DogDog({})
This step

A subclass can reuse behavior from its parent and add or override methods. Attribute lookup follows the inheritance hierarchy.

Picture inheritance

A subclass can reuse behavior from its parent and add or override methods. Attribute lookup follows the inheritance hierarchy.

Image brief

Alt text
A diagram for Python Inheritance. Instructions: class Animal: def speak(self): return "sound" class Dog(Animal): def speak(self): return 'woof' pet = Dog() print(pet.speak()). Displayed result: woof. The final example bindings are Animal --> object 2 (type); Dog --> object 4 (type); pet --> object 5 (Dog). object 2: class Animal; object 4: class Dog; object 5: Dog({})
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Inheritance". Teaching focus: A subclass can reuse behavior from its parent and add or override methods. Attribute lookup follows the inheritance hierarchy. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS class Animal: def speak(self): return "sound" class Dog(Animal): def speak(self): return 'woof' pet = Dog() print(pet.speak()) | v DISPLAYED RESULT woof FINAL BINDINGS Animal --> object 2 (type) Dog --> object 4 (type) pet --> object 5 (Dog) object 2: class Animal object 4: class Dog object 5: Dog({}) Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Inheritance. Instructions: class Animal: def speak(self): return "sound" class Dog(Animal): def speak(self): return 'woof' pet = Dog() print(pet.speak()). Displayed result: woof. The final example bindings are Animal --> object 2 (type); Dog --> object 4 (type); pet --> object 5 (Dog). object 2: class Animal; object 4: class Dog; object 5: Dog({})

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
class Animal:
    def speak(self):
        return "sound"
class Dog(Animal):
    def speak(self):
        return 'woof'
pet = Dog()
print(pet.speak())
Live state

Top-level scope

Animal→Object 2
Dog→Object 4
pet→Object 5
Object 2 · typeclass AnimalObject 4 · typeclass DogObject 5 · DogDog({})
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
woof

What do you expect this program to print?

What does this example display?

class Animal:
    def speak(self):
        return "sound"
class Dog(Animal):
    def speak(self):
        return 'woof'
pet = Dog()
print(pet.speak())
What does this example display?
Choose one answer. You can try again.

Answer and explanation

woof

Follow the printed values in order. The output is: woof

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

A subclass can reuse behavior from its parent and add or override methods. Attribute lookup follows the inheritance hierarchy.

A subclass can reuse behavior from its parent and add or override methods. Attribute lookup follows the inheritance hierarchy.

More examples and reference

In programming, Inheritance is a way to make one class (a blueprint for an object) get all the features (code) of another class — so you don't have to repeat yourself!

This helps us reuse code and build on top of existing code instead of starting from scratch.

What Is a Class?

A class is like a template or blueprint. It defines how something should behave. We use classes to create objects.

What Is Inheritance?

Inheritance means that one class (called the child) can use the code of another class (called the parent).

Example 1: Basic Inheritance

Let’s say we have a general class called Animal that has a function speak(). We can create a class Dog that inherits everything from Animal.

class Animal:
    def speak(self):
        print("Animal speaks")

class Dog(Animal):
    pass

d = Dog()
d.speak()
Animal speaks

Explanation:

  • Animal is the parent class with a method speak().
  • Dog is the child class — it uses pass which means "don’t add anything new right now."
  • We create d as a Dog object, but it can still use the speak() method from Animal.

Example 2: Child Adds Its Own Method

class Animal:
    def speak(self):
        print("Animal speaks")

class Dog(Animal):
    def bark(self):
        print("Dog barks")

d = Dog()
d.speak()
d.bark()
Animal speaks
Dog barks

Explanation:

  • Dog gets speak() from Animal.
  • Dog also has its own method bark().
  • We can use both methods from the Dog object.

Example 3: Child Overrides Parent Method

class Animal:
    def speak(self):
        print("Animal speaks")

class Dog(Animal):
    def speak(self):
        print("Dog says Woof!")

d = Dog()
d.speak()
Dog says Woof!

Explanation:

  • Dog has its own speak() method.
  • This overrides (replaces) the speak() from the Animal class.
  • So when we call d.speak(), it uses Dog’s version instead.

Why Use Inheritance?

  • Reusability: You don’t need to rewrite the same code.
  • Organized code: Parent classes can hold common code, and child classes can add or change specific behavior.

Conclusion

Inheritance helps us build smarter programs by reusing and extending code. Once you understand how a child class can use and even change what it gets from a parent class, you’ll be able to organize your Python programs better and avoid repeating yourself.

Don’t worry if this feels new — with practice, inheritance will feel natural!

What to remember

Connect what you noticed in the example with the idea below.

Remember

A subclass can reuse behavior from its parent and add or override methods. Attribute lookup follows the inheritance hierarchy.

Watch out for this idea

A subclass must rewrite every method of its parent.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Multiple Inheritance →