Inheritance is an important concept of Object Oriented Programming (OOP). It allows one class to acquire attributes and methods from another class. Inheritance helps us reuse existing code and create relationships between classes.
Inheritance allows a child class to reuse attributes and methods defined in a parent class.
class Animal:
def speak(self):
print("Animal makes a sound")
class Dog(Animal):
pass
dog = Dog()
dog.speak()
Output:
Animal makes a sound
The class being inherited from is called the parent class, base class, or superclass. The class that inherits from it is called the child class, derived class, or subclass.
class Person:
def show_person(self):
print("I am a person")
class Student(Person):
pass
To inherit from another class, place the parent class inside parentheses after the child class name.
class Parent:
pass
class Child(Parent):
pass
When one child class inherits from one parent class, it is called single inheritance.
class Animal:
def eat(self):
print("Animal eats")
class Dog(Animal):
def bark(self):
print("Dog barks")
dog = Dog()
dog.eat()
dog.bark()
Output:
Animal eats Dog barks
A child object can directly call a method inherited from its parent.
class Vehicle:
def start(self):
print("Vehicle started")
class Car(Vehicle):
pass
car = Car()
car.start()
Output:
Vehicle started
A child class can add its own methods in addition to inherited methods.
class Animal:
def eat(self):
print("Eating")
class Dog(Animal):
def bark(self):
print("Barking")
dog = Dog()
dog.eat()
dog.bark()
Output:
Eating Barking
If a child class does not define its own __init__() method, it can inherit the parent's initializer.
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
pass
student = Student("Rahul")
print(student.name)
Output:
Rahul
If the child class defines its own __init__(), it replaces the inherited initializer for that child class. The parent initializer can be called explicitly when needed.
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, name, course):
self.name = name
self.course = course
student = Student(
"Rahul",
"Python"
)
print(student.name)
print(student.course)
Output:
Rahul Python
The super() function provides access to methods and behavior from a parent class. It is commonly used to call the parent's initializer.
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, name, course):
super().__init__(name)
self.course = course
student = Student(
"Rahul",
"Python"
)
print(student.name)
print(student.course)
Output:
Rahul Python
A child class can define a method with the same name as a method in the parent class. The child implementation is then used when the method is called on a child object.
class Animal:
def sound(self):
print("Animal sound")
class Dog(Animal):
def sound(self):
print("Bark")
dog = Dog()
dog.sound()
Output:
Bark
When a child overrides a method, super() can be used to call the parent implementation as part of the child implementation.
class Animal:
def sound(self):
print("Animal sound")
class Dog(Animal):
def sound(self):
super().sound()
print("Bark")
dog = Dog()
dog.sound()
Output:
Animal sound Bark
When a class inherits from another child class, it creates a chain of inheritance. This is called multilevel inheritance.
class Grandparent:
def show_grandparent(self):
print("Grandparent")
class Parent(Grandparent):
def show_parent(self):
print("Parent")
class Child(Parent):
def show_child(self):
print("Child")
obj = Child()
obj.show_grandparent()
obj.show_parent()
obj.show_child()
Output:
Grandparent Parent Child
When one child class inherits from more than one parent class, it is called multiple inheritance.
class Father:
def skill1(self):
print("Driving")
class Mother:
def skill2(self):
print("Cooking")
class Child(Father, Mother):
pass
child = Child()
child.skill1()
child.skill2()
Output:
Driving Cooking
When multiple child classes inherit from the same parent class, it is called hierarchical inheritance.
class Animal:
def eat(self):
print("Eating")
class Dog(Animal):
def bark(self):
print("Barking")
class Cat(Animal):
def meow(self):
print("Meowing")
dog = Dog()
cat = Cat()
dog.eat()
dog.bark()
cat.eat()
cat.meow()
Output:
Eating Barking Eating Meowing
Hybrid inheritance is a combination of two or more inheritance patterns. Python supports complex inheritance structures.
class A:
def show_a(self):
print("A")
class B(A):
def show_b(self):
print("B")
class C(A):
def show_c(self):
print("C")
class D(B, C):
pass
obj = D()
obj.show_a()
obj.show_b()
obj.show_c()
Output:
A B C
Python uses Method Resolution Order (MRO) to determine the order in which classes are searched for attributes and methods. This is especially important with multiple inheritance.
class A:
pass
class B(A):
pass
class C(A):
pass
class D(B, C):
pass
print(D.mro())
The result shows the order in which Python searches the classes.
class A:
def show(self):
print("A")
class B(A):
def show(self):
print("B")
class C(A):
def show(self):
print("C")
class D(B, C):
pass
obj = D()
obj.show()
Output:
B
Python follows the MRO and finds the method in B before continuing to C.
The issubclass() function checks whether a class is a subclass of another class. It returns True or False.
class Animal:
pass
class Dog(Animal):
pass
print(issubclass(Dog, Animal))
Output:
True
An object of a child class is also considered an instance of its parent class.
class Animal:
pass
class Dog(Animal):
pass
dog = Dog()
print(isinstance(dog, Dog))
print(isinstance(dog, Animal))
Output:
True True
Inheritance is useful when there is a clear "is-a" relationship between classes.
Dog is an Animal Car is a Vehicle Student is a Person
If the relationship is instead "has-a", composition is often a better design choice.
Car has an Engine Student has an Address
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def display_person(self):
print("Name:", self.name)
print("Age:", self.age)
class Student(Person):
def __init__(
self,
name,
age,
course
):
super().__init__(name, age)
self.course = course
def display_student(self):
self.display_person()
print("Course:", self.course)
student = Student(
"Rahul",
20,
"Python"
)
student.display_student()
Output:
Name: Rahul Age: 20 Course: Python
| Type | Description |
|---|---|
| Single | One child inherits from one parent. |
| Multiple | One child inherits from multiple parents. |
| Multilevel | Inheritance occurs through multiple levels. |
| Hierarchical | Multiple children inherit from one parent. |
| Hybrid | Combination of multiple inheritance patterns. |
Question: Which function is commonly used to call a parent class method or initializer?