Inheritance is a key concept in Object-Oriented Programming (OOP) that allows a class to inherit attributes and methods from another class. This enables code reuse, simplifies program structure, and allows for the creation of more complex and flexible applications. In this tutorial, we’ll cover everything you need to know about inheritance in Python, including types of inheritance, syntax, examples, and real-world applications.
In Python, inheritance allows one class, known as the child (or derived) class, to inherit attributes and methods from another class, known as the parent (or base) class. This creates a relationship between classes and allows the child class to reuse and extend the functionalities of the parent class. Inheritance promotes code reusability and helps structure complex programs.
Inheritance offers several benefits in Python:
In Python, a child class is created by specifying the parent class in parentheses after the child class name.
class ParentClass:
# Parent class code
class ChildClass(ParentClass):
# Child class code
class Animal:
def speak(self):
return "Animal sound"
class Dog(Animal):
def bark(self):
return "Woof!"
dog = Dog()
print(dog.speak()) # Output: Animal sound
print(dog.bark()) # Output: Woof!
In this example, Dog
is a child class that inherits the speak
method from the Animal
parent class.
Python supports multiple types of inheritance, each useful for different scenarios.
Single inheritance occurs when a child class inherits from one parent class.
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
def bark(self):
return "Woof!"
dog = Dog()
print(dog.speak()) # Output: Some sound
print(dog.bark()) # Output: Woof!
In multiple inheritance, a child class inherits from more than one parent class. Python supports multiple inheritance, but it should be used carefully to avoid complexity.
class Flyer:
def fly(self):
return "Flying"
class Swimmer:
def swim(self):
return "Swimming"
class Duck(Flyer, Swimmer):
pass
duck = Duck()
print(duck.fly()) # Output: Flying
print(duck.swim()) # Output: Swimming
In multilevel inheritance, a class inherits from a child class, creating multiple levels of inheritance.
class Animal:
def eat(self):
return "Eating"
class Dog(Animal):
def bark(self):
return "Barking"
class Puppy(Dog):
def play(self):
return "Playing"
puppy = Puppy()
print(puppy.eat()) # Output: Eating
print(puppy.bark()) # Output: Barking
print(puppy.play()) # Output: Playing
In hierarchical inheritance, multiple child classes inherit from a single parent class.
class Animal:
def breathe(self):
return "Breathing"
class Dog(Animal):
def bark(self):
return "Woof!"
class Cat(Animal):
def meow(self):
return "Meow!"
dog = Dog()
cat = Cat()
print(dog.breathe()) # Output: Breathing
print(dog.bark()) # Output: Woof!
print(cat.breathe()) # Output: Breathing
print(cat.meow()) # Output: Meow!
Method overriding allows a child class to provide a specific implementation of a method that is already defined in its parent class.
class Animal:
def speak(self):
return "Animal sound"
class Dog(Animal):
def speak(self): # Overriding the parent method
return "Woof!"
dog = Dog()
print(dog.speak()) # Output: Woof!
In this example, the Dog
class overrides the speak
method of the Animal
class.
super()
FunctionThe super()
function allows a child class to call a method from its parent class. This is particularly useful when you want to extend or modify the behavior of a parent method.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Some sound"
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # Calling the parent constructor
self.breed = breed
def speak(self):
return f"{super().speak()} and Woof!" # Extending the parent method
dog = Dog("Buddy", "Golden Retriever")
print(dog.name) # Output: Buddy
print(dog.speak()) # Output: Some sound and Woof!
In this example, super()
allows Dog
to use the constructor and speak
method from Animal
while extending the speak
method.
While inheritance models an "is-a" relationship, composition models a "has-a" relationship. Composition is an alternative to inheritance that involves including objects of one class within another class rather than inheriting its properties.
class Engine:
def start(self):
return "Engine started"
class Car:
def __init__(self):
self.engine = Engine() # Car "has-a" engine
def start_car(self):
return self.engine.start()
car = Car()
print(car.start_car()) # Output: Engine started
In this example, Car
contains an Engine
object instead of inheriting from an engine
class, showing composition over inheritance.
In an employee management system, inheritance can be used to create different types of employees, such as Manager and Developer, that share common attributes and methods.
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def display_info(self):
print(f"Name: {self.name}, Salary: {self.salary}")
class Manager(Employee):
def __init__(self, name, salary, department):
super().__init__(name, salary)
self.department = department
def display_info(self):
super().display_info()
print(f"Department: {self.department}")
class Developer(Employee):
def __init__(self, name, salary, language):
super().__init__(name, salary)
self.language = language
def display_info(self):
super().display_info()
print(f"Language: {self.language}")
manager = Manager("Alice", 70000, "Sales")
developer = Developer("Bob", 60000, "Python")
manager.display_info()
# Output:
# Name: Alice, Salary: 70000
# Department: Sales
developer.display_info()
# Output:
# Name: Bob, Salary: 60000
# Language: Python
In a vehicle management system, different types of vehicles, like Car
and Truck
, can inherit common properties from a Vehicle
class.
class Vehicle:
def __init__(self, make, model):
self.make = make
self.model = model
def display_info(self):
print(f"Make: {self.make}, Model: {self.model}")
class Car(Vehicle):
def __init__(self, make, model, doors):
super().__init__(make, model)
self.doors = doors
def display_info(self):
super().display_info()
print(f"Doors: {self.doors}")
class Truck(Vehicle):
def __init__(self, make, model, capacity):
super().__init__(make, model)
self.capacity = capacity
def display_info(self):
super().display_info()
print(f"Capacity: {self.capacity} tons")
car = Car("Toyota", "Camry", 4)
truck = Truck("Ford", "F-150", 10)
car.display_info()
# Output:
# Make: Toyota, Model: Camry
# Doors: 4
truck.display_info()
# Output:
# Make: Ford, Model: F-150
# Capacity: 10 tons
Inheritance is a fundamental concept in Python that enables you to create classes that share common attributes and methods with parent classes, promoting code reusability and organization. By mastering inheritance, you can structure your code to represent real-world relationships, allowing for a cleaner, more modular, and adaptable codebase.
With inheritance, you can:
Ready to implement inheritance in your Python projects? Try building your class hierarchies with inheritance to see how it improves code structure and efficiency. Happy coding!