What Is Inheritance in Python?
Inheritance in Python is a way for one class to use code from another class.
This helps you reuse methods and attributes instead of writing the same code again. It is a common idea in object-oriented programming, so it makes more sense if you already understand classes and objects in Python.
Here is the basic idea:
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
pass
pet = Dog()
print(pet.speak())
Output:
Some sound
Dog does not define speak(), but it can still use it because Dog inherits from Animal.
What inheritance means
Inheritance lets one class use code from another class.
- The new class is often called the child class or subclass
- The class it inherits from is often called the parent class or base class
- It helps avoid repeating the same methods and attributes
In simple terms:
Animalis the parent classDogis the child classDogcan use behavior defined inAnimal
If you are new to this topic, it also helps to know what a class is in Python and what an object is in Python.
Why beginners use inheritance
Beginners usually use inheritance for simple code reuse.
It is useful when:
- You want to reuse code instead of writing the same method again
- You have related classes that share common behavior
- You want to keep code organized when objects are similar
- You want updates in shared code to affect multiple child classes
For example, if both Dog and Cat are animals, they may both need methods like eat() or sleep(). Instead of writing those methods twice, you can put them in Animal.
Basic inheritance syntax
To create a child class, put the parent class name in parentheses after the child class name.
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
pass
This line is the key part:
class Dog(Animal):
It means:
Doginherits fromAnimalDoggets access to methods and attributes fromAnimalDogcan also define its own methods
If you want a refresher on class methods, see basic methods in Python classes.
What a child class can do
A child class can do several useful things:
- Use parent methods without rewriting them
- Add new methods that only belong to the child class
- Override a parent method with a new version
- Call parent behavior when needed
Here is a simple example:
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
def fetch(self):
return "The dog runs after the ball"
pet = Dog()
print(pet.speak())
print(pet.fetch())
Output:
Some sound
The dog runs after the ball
Dog inherited speak() from Animal and added its own method, fetch().
Simple example to include
Here is a slightly fuller example that shows both inherited behavior and new child behavior:
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
def fetch(self):
return "Fetching the stick"
dog = Dog()
print(dog.speak())
print(dog.fetch())
Output:
Some sound
Fetching the stick
What happens here:
Animaldefinesspeak()Doginherits fromAnimalDogdoes not need to writespeak()againDogadds a new method calledfetch()
This is a good example of how inheritance reduces repetition.
Method overriding
A child class can replace a parent method with its own version. This is called overriding.
Use overriding when classes share a general idea but need different behavior.
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
Even though Dog inherits from Animal, it uses its own speak() method because the child version overrides the parent version.
Using super() at a beginner level
super() lets a child class call a method from the parent class.
Beginners often see this in the __init__ method, which is explained in the __init__ method in Python.
Example:
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("Max", "Labrador")
print(pet.name)
print(pet.breed)
Output:
Max
Labrador
Why use super() here?
Animal.__init__()setsself.nameDog.__init__()still wants that setup to happensuper().__init__(name)runs the parent setup first
This is a common and useful pattern.
When inheritance is a good fit
Inheritance works best when there is a clear parent-child relationship.
Good examples:
Dogis anAnimalCatis anAnimal
Use inheritance when:
- Multiple classes share the same fields or methods
- The child classes are specialized versions of a more general class
Be careful not to force inheritance just to share a small amount of code. Sometimes simple separate classes are easier to read.
Common beginner confusion
A few points often cause confusion:
- Inheritance does not copy and paste code into the child class
- The child class can access parent behavior because of the class relationship
- A child class can have extra behavior that the parent does not have
- Not every related class should use inheritance
It also helps to remember that inheritance is about classes, not about creating objects. If that part feels unclear, review object-oriented programming in Python.
Common mistakes
Beginners often make these mistakes:
- Confusing inheritance with creating an object
- Thinking the child class must rewrite every parent method
- Not understanding the difference between parent class and child class
- Using inheritance when separate classes would be clearer
If you want to inspect what an object or class can do, these tools can help:
print(type(obj))
print(isinstance(obj, Animal))
print(isinstance(obj, Dog))
print(dir(obj))
help(Dog)
What they do:
type(obj)shows the object's classisinstance(obj, Animal)checks if the object is treated as anAnimalisinstance(obj, Dog)checks if it is specifically aDogdir(obj)lists available attributes and methodshelp(Dog)shows information about the class
FAQ
What is inheritance in Python in simple words?
It is a way for one class to use code from another class.
What is the parent class?
The parent class is the class that provides methods and attributes to another class.
What is the child class?
The child class inherits from the parent class and can also add or change behavior.
Can a child class change a parent method?
Yes. This is called overriding.
Should beginners always use inheritance?
No. Use it when there is a clear shared relationship between classes.
See also
- Object-oriented programming in Python explained
- Python classes and objects explained
- How to inherit from a class in Python
- What is a method in Python?
Next, learn how classes and objects work so inheritance makes more sense in real code.