Introduction
Inheritance is one of the most important concepts in Object-Oriented Programming (OOP). It allows a class to inherit properties and methods from another class, making code more reusable, organized, and easier to maintain.
For example, if you have a Vehicle class containing common properties such as speed and fuel type, classes like Car, Bike, and Truck can inherit these properties instead of redefining them.
What is Inheritance in Python?
Inheritance is a mechanism that allows one class to acquire the properties and methods of another class.
The class being inherited from is called the:
- Parent Class
- Base Class
- Super Class
The class that inherits is called the:
- Child Class
- Derived Class
- Sub Class
Example:
class Parent:
pass
class Child(Parent):
pass
Here:
- Parent is the parent class.
- Child inherits from Parent.
Why Use Inheritance?
Inheritance provides several advantages:
1. Code Reusability
Write code once and reuse it in multiple classes.
2. Reduced Redundancy
Avoid repeating common functionality.
3. Better Organization
Classes can be organized hierarchically.
4. Easier Maintenance
Changes in the parent class automatically affect child classes.
5. Scalability
Large applications become easier to manage.
Syntax
Basic syntax:
class ParentClass:
# parent code
class ChildClass(ParentClass):
# child code
Example:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
pass
dog = Dog()
dog.speak()
Output:
Explanation:
The Dog class inherits the speak() method from the Animal class.
Parent and Child Class Example
class Person:
def display(self):
print("I am a person")
class Student(Person):
pass
student = Student()
student.display()
Output:
The child class automatically gains access to parent methods.
Inheriting Attributes
Inheritance works with attributes as well.
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
pass
student = Student("John")
print(student.name)
Output:
Explanation:
The Student class inherits the constructor and attribute from Person.
Using super() in Inheritance
The super() function is used to call methods from the parent class.
Example:
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(
"John",
"Python"
)
print(student.name)
print(student.course)
Output:
Python
Benefits of super():
- Avoids duplicate code
- Calls parent constructors easily
- Improves maintainability
Method Overriding
A child class can redefine a parent method.
This is called method overriding.
Example:
class Animal:
def sound(self):
print("Animal makes sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
dog = Dog()
dog.sound()
Output:
Explanation:
The child method overrides the parent method.
Types of Inheritance in Python
Python supports multiple types of inheritance.
1. Single Inheritance
A child class inherits from one parent class.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
pass
Diagram:
Animal
|
Dog
2. Multiple Inheritance
A child class inherits from multiple parent classes.
class Father:
def skills(self):
print("Programming")
class Mother:
def talents(self):
print("Music")
class Child(Father, Mother):
pass
child = Child()
child.skills()
child.talents()
Output:
Music
Diagram:
Father Mother
\ /
Child
3. Multilevel Inheritance
A class inherits from a class that already inherits another class.
class Grandparent:
def house(self):
print("Family House")
class Parent(Grandparent):
pass
class Child(Parent):
pass
child = Child()
child.house()
Output:
Diagram:
Grandparent
|
Parent
|
Child
4. Hierarchical Inheritance
Multiple child classes inherit from one parent class.
class Animal:
def eat(self):
print("Eating")
class Dog(Animal):
pass
class Cat(Animal):
pass
Diagram:
Animal
/ \
Dog Cat
5. Hybrid Inheritance
Combination of multiple inheritance types.
Example:
class A:
pass
class B(A):
pass
class C(A):
pass
class D(B, C):
pass
Diagram:
A
/ \
B C
\ /
D
Real-Life Examples:
1. Employee Management System
class Employee:
def __init__(self, name):
self.name = name
def display(self):
print("Employee:", self.name)
class Manager(Employee):
def manage_team(self):
print("Managing team")
manager = Manager("David")
manager.display()
manager.manage_team()
Output:
Managing team
Explanation:
- Employee provides common functionality.
- Manager inherits and adds extra features.
2. Vehicle System
class Vehicle:
def start(self):
print("Vehicle Started"
class Car(Vehicle):
def drive(self):
print("Driving Car")
car = Car()
car.start()
car.drive()
Output:
Driving Car
Inheritance allows all vehicles to share common behaviors.
Advantages of Inheritance
| Advantage | Description |
|---|---|
| Code Reusability | Reuse parent class code |
| Less Redundancy | Avoid duplicate code |
| Better Organization | Hierarchical structure |
| Easier Maintenance | Update parent class once |
| Scalability | Suitable for large projects |
Inheritance vs Composition
| Inheritance | Composition |
|---|---|
| IS-A relationship | HAS-A relationship |
| Reuses parent class | Uses objects of another class |
| Strong coupling | Loose coupling |
| Example: Dog is an Animal | Car has an Engine |
Common Mistakes
1. Forgetting Parent Constructor
Incorrect:
class Student(Person):
def __init__(self, name):
pass
Correct:
class Student(Person):
def __init__(self, name):
super().__init__(name)
2. Overriding Without Need
Don’t override methods unless behavior needs to change.
3. Creating Deep Inheritance Chains
Bad:
A → B → C → D → E → F
This makes code difficult to understand.
4. Forgetting Parentheses
Incorrect:
class Student:
Correct:
class Student(Person):
5. Misusing Multiple Inheritance
Use multiple inheritance only when necessary.
Conclusion
Inheritance is one of the core pillars of Object-Oriented Programming in Python. It enables classes to inherit attributes and methods from other classes.
Python supports multiple inheritance types, including single, multiple, multilevel, hierarchical, and hybrid inheritance, making it flexible for different application designs.
Python Inheritance – Interview Questions
Q 1: What is inheritance in Python?
Q 2: What is a parent (base) class?
Q 3: What is a child (derived) class?
Q 4: Can a child class override parent methods?
Q 5: What types of inheritance does Python support?
Python Inheritance – Objective Questions (MCQs)
Q1. What is inheritance in Python?
Q2. Which of the following is the correct syntax for inheritance in Python?
Q3. What will be the output of the following code?
class A:
def show(self):
print("Class A")
class B(A):
pass
obj = B()
obj.show()
Q4. Which function can be used to call the parent class constructor inside a derived class?
Q5. What type of inheritance is shown below?
class A: pass
class B(A): pass
class C(B): pass