How to Inherit from a Class in Python
Inheritance lets you create a new class from an existing class.
This is useful when two classes should share code. The new class can reuse methods and attributes from the existing class, and it can also add new behavior or replace old behavior when needed.
If you are new to classes, it may help to first read how to create a class in Python and Python classes and objects explained.
Quick answer
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
pass
pet = Dog()
print(pet.speak())
Output:
Some sound
Put the parent class name inside parentheses after the child class name. The child class then gets the parent class methods.
What this page helps you do
After reading this page, you will be able to:
- Create a class that inherits from another class
- Reuse methods and attributes from a parent class
- Understand parent class and child class in simple terms
- Know when to add new behavior or replace inherited behavior
What inheritance means
Inheritance lets one class use code from another class.
In simple terms:
- The existing class is the parent class
- The new class is the child class
- The child class can use parent methods without rewriting them
Here is a simple example:
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
pass
dog = Dog()
print(dog.speak())
Dog inherits from Animal, so dog.speak() works even though Dog does not define its own speak() method.
If you want a simple definition, see what inheritance means in Python.
Basic inheritance syntax
To make one class inherit from another:
- Write the child class name first
- Put the parent class name in parentheses
- Use
passif the child class does not add anything yet - Create an object from the child class and call inherited methods
class Parent:
def greet(self):
return "Hello from Parent"
class Child(Parent):
pass
obj = Child()
print(obj.greet())
Output:
Hello from Parent
The important line is:
class Child(Parent):
That tells Python that Child should inherit from Parent.
If you need a refresher on making instances, see how to create an object in Python.
Add new methods in the child class
A child class can keep parent methods and also add its own methods.
Use this when the child needs extra behavior.
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
def wag_tail(self):
return "Tail is wagging"
dog = Dog()
print(dog.speak())
print(dog.wag_tail())
Output:
Some sound
Tail is wagging
What happens here:
Doginheritsspeak()fromAnimalDogadds a new method calledwag_tail()- A
Dogobject can use both methods
If you want more practice writing methods, see how to add methods to a class in Python.
Override a parent method
Sometimes the child class should behave differently from the parent class.
To do that, write a method in the child class with the same name. This is called overriding.
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
def speak(self):
return "Woof"
animal = Animal()
dog = Dog()
print(animal.speak())
print(dog.speak())
Output:
Some sound
Woof
Python uses the child version when you call that method on a child object.
Use overriding when:
- The parent has a general behavior
- The child needs a more specific version
Use super() to call the parent version
super() lets the child class call a method from the parent class.
This is very common inside __init__, where objects are set up with starting attributes.
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
pet = Dog("Buddy", "Labrador")
print(pet.name)
print(pet.breed)
Output:
Buddy
Labrador
Why use super() here:
- The parent class already knows how to set
name - The child class does not need to repeat that code
- The child class can add its own extra setup, such as
breed
If __init__ is new to you, read the __init__ method in Python explained.
Simple example with __init__
A common pattern is:
- The parent class sets shared attributes
- The child class calls
super().__init__() - The child class adds its own attributes
class Vehicle:
def __init__(self, brand):
self.brand = brand
def show_brand(self):
return f"Brand: {self.brand}"
class Car(Vehicle):
def __init__(self, brand, doors):
super().__init__(brand)
self.doors = doors
def show_doors(self):
return f"Doors: {self.doors}"
car = Car("Toyota", 4)
print(car.show_brand())
print(car.show_doors())
Output:
Brand: Toyota
Doors: 4
This works because:
Carinherits fromVehicleVehiclehandles the sharedbrandattributeCaradds thedoorsattribute
When inheritance is useful
Inheritance is useful when:
- Several classes share the same data or methods
- You want to avoid copying the same code into many classes
- You want related classes with small differences
Common examples:
AnimalandDogVehicleandCarUserandAdminUser
Inheritance is usually a good choice when the child class is a more specific version of the parent class.
Common beginner mistakes
Here are some common problems beginners run into.
Forgetting the parent class in parentheses
This does not create inheritance:
class Animal:
def speak(self):
return "Some sound"
class Dog:
pass
Dog will not get the speak() method because it does not inherit from Animal.
Correct version:
class Dog(Animal):
pass
Overriding __init__ but not calling super().__init__()
If the parent class sets important attributes, the child class may break if it skips the parent setup.
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, breed):
self.breed = breed
dog = Dog("Labrador")
print(dog.name)
This causes an error because name was never created.
A better version is:
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
dog = Dog("Buddy", "Labrador")
print(dog.name)
print(dog.breed)
Misspelling a method name
If you want to override a method, the name must match exactly.
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
def speek(self):
return "Woof"
Here, speek() does not override speak().
Expecting the parent class to use child-only attributes automatically
The parent class only knows about attributes that have been created.
If the parent method uses self.name, then name must exist before that method is called.
How to check that inheritance is working
If you are not sure whether inheritance is working, try these checks.
1. Create an object from the child class
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
pass
pet = Dog()
print(type(pet))
2. Call a method defined in the parent class
print(pet.speak())
If this works, the child object is using the parent method.
3. Print the object attributes
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
pet = Dog("Buddy", "Labrador")
print(pet.__dict__)
Example output:
{'name': 'Buddy', 'breed': 'Labrador'}
4. Use isinstance()
print(isinstance(pet, Dog))
print(isinstance(pet, Animal))
Both can be True because a Dog object is also an Animal.
5. Useful debugging commands
These can help when something is not working as expected:
print(type(obj))
print(isinstance(obj, ParentClass))
print(obj.__dict__)
print(dir(obj))
help(ChildClass)
They are useful for checking:
- What type the object is
- Whether Python sees it as an instance of the parent class
- Which attributes exist
- Which methods are available
FAQ
What is the difference between a parent class and a child class?
A parent class contains shared code. A child class inherits that code and can also add or change behavior.
Do I always need super() when using inheritance?
No. It is most useful when the parent class has an __init__ method or another method you still want to use.
Can a child class use parent methods without rewriting them?
Yes. That is one of the main reasons to use inheritance.
What happens if the child class has a method with the same name as the parent?
The child version is used. This is called overriding.
Is inheritance the same as creating an object from a class?
No. Inheritance creates a new class from another class. Creating an object makes an instance of a class.