Inheritance in Python: Complete Guide | FACE Prep

Inheritance in Python: Complete Guide | FACE Prep

Inheritance in Python: A Complete Guide for Beginners

Inheritance is one of the core principles of Object-Oriented Programming (OOP) in Python. It allows a class (called the child class) to inherit properties and methods from another class (called the parent class). This powerful feature enhances code reusability, reduces redundancy, and improves code organization. In this guide, we’ll explain the concept of inheritance, its types, and practical examples to help you grasp this fundamental OOP principle.

What is Inheritance in Python?

Inheritance is a mechanism in Python that allows a child class to inherit properties and behaviors (methods) from a parent class. It provides an efficient way to create new classes based on existing ones, allowing you to reuse the code and extend its functionality. This leads to more concise, maintainable, and scalable code.In Python, the child class automatically has access to the methods and attributes of the parent class. This simplifies the creation of new classes, especially when building more complex systems or frameworks.

Why Do We Need Inheritance?

  • Code Reusability: Inheritance allows you to create a new class by reusing the existing code in a parent class.
  • Extensibility: You can add or modify behavior in the child class without changing the parent class, making your code more flexible.
  • Transitive Nature: If class B inherits class A, then all subclasses of B can also inherit class A, which saves time and effort when managing large codebases.

How to Implement Inheritance in Python

To implement inheritance in Python, you need to define a parent class and a child class. The child class is created by passing the parent class as an argument.

Syntax for Creating a Parent Class

A parent class is a regular class with attributes and methods that can be inherited by child classes.
python
# Parent class class Car: length = '470 cm'def drive(self): print(“I can drive at a speed of 60 km/h”)
In the above example, the Car class has a length attribute and a drive method, both of which can be inherited by child classes.

Syntax for Creating a Child Class

A child class is defined by passing the parent class as an argument.
python
# Child class inheriting from Car class class RaceCar(Car): pass # Inherits everything from the parent class

Accessing Parent Class Methods in the Child Class

You can access the parent class methods from the child class by creating an object of the child class.
python
# Main code maruti = RaceCar() maruti.drive() # Calling the drive method from the parent class
Output:
css
I can drive at a speed of 60 km/h
In this example, the RaceCar class inherits the drive method from the Car class and can use it directly.

Types of Inheritance in Python

Python supports several types of inheritance, which allow for different relationships between the parent and child classes. Here’s a look at the different types:

1. Single Inheritance

In single inheritance, a child class inherits from only one parent class. This is the simplest form of inheritance.

Example of Single Inheritance

python
class Car: def drive(self): print("I can drive at a speed of 60 km/h")class RaceCar(Car): def drive_fast(self): print(“I can drive at a speed of 100 km/h”)# Main code maruti = RaceCar() maruti.drive() # Inherited from Car maruti.drive_fast() # Defined in RaceCar
Output:
css
I can drive at a speed of 60 km/h I can drive at a speed of 100 km/h

2. Multiple Inheritance

In multiple inheritance, a child class inherits from more than one parent class. This allows a class to combine features from multiple classes.

Example of Multiple Inheritance

python
class Car1: def drive1(self): print("I can drive at a speed of 60 km/h")class Car2: def drive2(self): print(“I can drive at a speed of 80 km/h”)class RaceCar(Car1, Car2): pass# Main code racecar = RaceCar() racecar.drive1() # Inherited from Car1 racecar.drive2() # Inherited from Car2
Output:
css
I can drive at a speed of 60 km/h I can drive at a speed of 80 km/h

3. Multilevel Inheritance

In multilevel inheritance, a child class inherits from a parent class, and then another class inherits from the child class, forming a hierarchy.

Example of Multilevel Inheritance

python
class Car: def drive(self): print("I can drive at a speed of 60 km/h")class RaceCar(Car): def speed(self): print(“I can drive at a speed of 100 km/h”)class FormulaCar(RaceCar): def turbo(self): print(“I can drive at a speed of 200 km/h”)# Main code formula = FormulaCar() formula.drive() # Inherited from Car formula.speed() # Inherited from RaceCar formula.turbo() # Defined in FormulaCar
Output:
css
I can drive at a speed of 60 km/h I can drive at a speed of 100 km/h I can drive at a speed of 200 km/h

4. Hierarchical Inheritance

In hierarchical inheritance, multiple child classes inherit from a single parent class. This is commonly used when multiple subclasses need to share common functionality.

Example of Hierarchical Inheritance

python
class Car: def drive(self): print("I can drive at a speed of 60 km/h")class RaceCar(Car): def speed(self): print(“I can drive at a speed of 100 km/h”)class Sedan(Car): def comfort(self): print(“I offer great comfort and luxury”)# Main code sedan = Sedan() sedan.drive() # Inherited from Car sedan.comfort() # Defined in Sedan
Output:
css
I can drive at a speed of 60 km/h I offer great comfort and luxury

5. Hybrid Inheritance

Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance. It combines features of more than one type of inheritance.

Conclusion:

Inheritance is a fundamental feature of Python that promotes code reuse and extensibility. By leveraging inheritance, you can build more flexible and maintainable code. It allows you to create classes that inherit properties and behaviors from other classes, making your code cleaner and easier to manage. Click Here to Know more our program! 
c