Inheritance in Python
Learn how one class can reuse the properties and methods of another class.
What is Inheritance?
Inheritance is an important concept of Object-Oriented Programming.
It allows one class to use the properties and methods of another class.
The existing class is called the Parent Class, and the new class is called the Child Class.
Example 📩🌍
Think about a company.
A general employee may have a name and employee ID. An IT employee can use those properties and also have additional properties such as programming skills.
Parent Class
Employee
Name
Employee ID
Child Class
IT Employee
Programming Skills
Technology
Inheritance Diagram
The child class gets the reusable functionality from the parent class.
Parent Class
Employee
name
employee_id
Child Class
ITEmployee
programming_skill
technology
Reusable Code
Child can use the parent class properties and methods.
Inheritance Syntax
class Parent:
# parent class code
class Child(Parent):
# child class code
The child class name is followed by the parent class inside brackets.
Simple Example
class Animal:
def eat(self):
print("Animal is eating")
class Dog(Animal):
def bark(self):
print("Dog is barking")
dog1 = Dog()
dog1.eat()
dog1.bark()
Output:
Animal is eating
Dog is barking
The Dog class gets the
eat() method from the
Animal class.
Parent Class and Child Class
The class from which another class inherits is called the parent or base class.
The class that inherits is called the child or derived class.
class Person:
def show_name(self):
print("Rahul")
class Student(Person):
def show_course(self):
print("Python")
student1 = Student()
student1.show_name()
student1.show_course()
Output:
Rahul
Python
Types of Inheritance
Python supports different inheritance patterns.
Inheritance
Different ways classes can be related to each other.
Single
One parent → One child
Multiple
Multiple parents → One child
Multilevel
Parent → Child → Grandchild
Hierarchical
One parent → Multiple children
Hybrid
Combination of inheritance types
1. Single Inheritance
In single inheritance, one child class inherits from one parent class.
class Animal:
def eat(self):
print("Eating")
class Dog(Animal):
def bark(self):
print("Barking")
dog1 = Dog()
dog1.eat()
dog1.bark()
Output:
Eating
Barking
2. Multiple Inheritance
In multiple inheritance, one child class inherits from more than one parent class.
class Father:
def father_property(self):
print("Father property")
class Mother:
def mother_property(self):
print("Mother property")
class Child(Father, Mother):
def child_property(self):
print("Child property")
child1 = Child()
child1.father_property()
child1.mother_property()
child1.child_property()
Output:
Father property
Mother property
Child property
3. Multilevel Inheritance
In multilevel inheritance, one class inherits from another class, and a third class inherits from the second class.
class Grandparent:
def property1(self):
print("Grandparent")
class Parent(Grandparent):
def property2(self):
print("Parent")
class Child(Parent):
def property3(self):
print("Child")
child1 = Child()
child1.property1()
child1.property2()
child1.property3()
Output:
Grandparent
Parent
Child
4. Hierarchical Inheritance
In hierarchical inheritance, multiple child classes inherit from one parent class.
class Animal:
def eat(self):
print("Eating")
class Dog(Animal):
def bark(self):
print("Barking")
class Cat(Animal):
def meow(self):
print("Meowing")
dog1 = Dog()
cat1 = Cat()
dog1.eat()
dog1.bark()
cat1.eat()
cat1.meow()
Output
Eating
Barking
Eating
Meowing
5. Hybrid Inheritance
Hybrid inheritance is a combination of two or more inheritance patterns.
It is generally used in more complex class designs.
class A:
def show_a(self):
print("Class A")
class B(A):
def show_b(self):
print("Class B")
class C(A):
def show_c(self):
print("Class C")
class D(B, C):
def show_d(self):
print("Class D")
obj = D()
obj.show_a()
obj.show_b()
obj.show_c()
obj.show_d()
Output
Class A
Class B
Class C
Class D
Using super()
The super() function is used to call
a method from the parent class.
class Parent:
def show(self):
print("Parent method")
class Child(Parent):
def show(self):
super().show()
print("Child method")
child1 = Child()
child1.show()
Output:
Parent method
Child method
Here super().show() calls the parent
class method.
Constructor in Inheritance
A child class can also have its own constructor.
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, name, course):
super().__init__(name)
self.course = course
student1 = Student("Rahul", "Python")
print(student1.name)
print(student1.course)
Output:
Rahul
Python
The child constructor uses super() to
call the parent constructor.
Method Overriding
A child class can provide its own version of a method that already exists in the parent class.
class Animal:
def sound(self):
print("Animal makes sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
dog1 = Dog()
dog1.sound()
Output:
Dog barks
The child class replaces the behavior of the inherited method.
Benefits of Inheritance
- Reuse existing code.
- Reduce duplicate code.
- Make code easier to maintain.
- Create parent-child relationships between classes.
- Support method overriding.
Parent Class vs Child Class
| Parent Class | Child Class |
|---|---|
| Existing class | New class |
| Provides reusable code | Reuses and can extend the code |
| Also called base class | Also called derived class |
Common Mistakes
- Forgetting to write the parent class in brackets.
- Confusing inheritance with object creation.
-
Forgetting
super()when the parent constructor needs to be called. - Accidentally overriding a parent method.
Practice Programs
-
Create a
Vehicleparent class and aCarchild class. -
Create an
Animalparent class andDogchild class. - Create an example of multiple inheritance.
- Create a multilevel inheritance example.
-
Use
super()to call a parent constructor. - Create an example of method overriding.
Summary
Inheritance allows one class to reuse the properties
and methods of another class. The existing class is
called the parent class and the new class is called
the child class. Python supports single, multiple,
multilevel, hierarchical and hybrid inheritance.
The super() function can be used to access
parent class functionality.