Python Inheritance Example

Inheritance is a way to reuse code in Python classes. In this example, a child class inherits from a parent class and can use the parent class method while also adding its own method.

This page gives you a simple, beginner-friendly example so you can see how inheritance works in real code.

Quick example

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

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

dog = Dog()
dog.speak()
dog.bark()

Output:

Animal sound
Woof!

In this example:

  • Animal is the parent class
  • Dog is the child class
  • Dog inherits the speak() method from Animal
  • Dog also has its own bark() method

What this example teaches

  • A child class can inherit from a parent class
  • The child class gets the parent class methods
  • The child class can also add new methods of its own
  • Inheritance helps avoid repeating code

If you are new to this topic, it may help to first read Python classes and objects explained or the glossary page on what a class is in Python.

Parent class and child class

Here is the same example again:

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

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

What each part means:

  • Animal is the parent class
  • Dog is the child class
  • Dog(Animal) means Dog inherits from Animal
  • Objects created from Dog can use methods from both classes

So when you create a Dog object, Python lets that object use:

  • speak() from Animal
  • bark() from Dog

This is the main idea behind inheritance in Python.

How the example works step by step

1. Define a parent class with one method

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

This creates a class named Animal.

Inside it, there is one method: speak().

2. Define a child class that inherits from the parent

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

Dog(Animal) means Dog is based on Animal.

Because of this, Dog gets access to the speak() method without writing it again.

3. Create an object from the child class

dog = Dog()

This creates an object named dog from the Dog class.

If you need more practice with this step, see how to create an object in Python.

4. Call an inherited method

dog.speak()

Even though speak() is written inside Animal, the dog object can still use it because Dog inherits from Animal.

5. Call a method defined in the child class

dog.bark()

This method belongs directly to Dog, so the dog object can use it too.

Why beginners use inheritance

Beginners often use inheritance when they want related classes to share behavior.

It helps because:

  • It groups related classes together
  • It lets you reuse existing code
  • It makes larger programs easier to organize
  • It is common in object-oriented programming

For example, if you had several animal classes, they could all inherit from one parent class instead of repeating the same methods in every class.

If you want to build your own class first, see how to create a class in Python.

What to watch out for

Here are some common beginner mistakes with inheritance:

  • Forgetting parentheses when inheriting from a class
  • Misspelling method names
  • Trying to call a child-only method on a parent object
  • Using inheritance when a simple class would be enough

Example of creating an object from the wrong class

This code causes a problem:

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

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

animal = Animal()
animal.bark()

Why it fails:

  • Animal has speak()
  • Animal does not have bark()
  • Only Dog objects have bark()

A correct version is:

dog = Dog()
dog.bark()

Common mistakes

Some common causes of confusion are:

  • Confusing parent class methods with child class methods
  • Creating an object from the wrong class
  • Using inheritance syntax incorrectly
  • Assuming inheritance copies code instead of sharing behavior

A useful thing to remember is this:

Inheritance does not copy and paste methods into the child class. Instead, the child class can access behavior from the parent class.

Helpful debugging checks

If your code is not working, these quick checks can help:

print(type(dog))
print(isinstance(dog, Dog))
print(isinstance(dog, Animal))
print(dir(dog))

What they do:

  • type(dog) shows the object's class
  • isinstance(dog, Dog) checks if dog is a Dog
  • isinstance(dog, Animal) checks if dog is also considered an Animal
  • dir(dog) shows available attributes and methods

Example:

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

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

dog = Dog()

print(type(dog))
print(isinstance(dog, Dog))
print(isinstance(dog, Animal))
print("speak" in dir(dog))
print("bark" in dir(dog))

Possible output:

<class '__main__.Dog'>
True
True
True
True

This shows that:

  • dog is a Dog
  • dog is also treated as an Animal
  • dog has both speak and bark

FAQ

What is inheritance in Python?

Inheritance lets one class use code from another class. A child class gets methods and attributes from a parent class.

Why would I use inheritance?

Use it when two classes are related and should share behavior. It helps reduce repeated code.

Can a child class add its own methods?

Yes. A child class can use inherited methods and also define new ones.

Can a child class change a parent method?

Yes. This is called overriding. The child class defines a method with the same name.

Here is a simple overriding example:

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

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

Now a Dog object will use its own speak() method instead of the parent version.

See also