Sitemap
Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Inheritance in Python: Building Relationships and Reusing Code

Introduction

3 min readJun 27, 2023

--

So lastly we were talking about Classes and Objects, it was a good article for starters. I wrote everything in detail for everyone to understand with examples, if you missed please have a look. Today we are about to discuss about Inheritance.

Get Rahul Pandey’s stories in your inbox

Join Medium for free to get updates from this writer.

In the realm of object-oriented programming (OOP), inheritance serves as a powerful mechanism that allows classes to inherit attributes and methods from other classes. It promotes code reuse, modularity, and the creation of hierarchical relationships between classes. In this article, we will explore the concept of inheritance in Python, understand its benefits, and delve into practical examples. Let’s embark on this inheritance journey!

Understanding Inheritance

Inheritance is the process by which one class acquires the attributes and methods of another class. The class that inherits is called the derived class or subclass, while the class being inherited from is called the base class or superclass. Inheritance facilitates the creation of specialized classes (subclasses) based on more generalized classes (superclasses), forming an “is-a” relationship.

Basic Inheritance Syntax

In Python, inheritance is accomplished by defining a class and specifying the base class inside parentheses after the class name. Let’s consider a basic example:

class Vehicle:  # Base class
def __init__(self, brand):
self.brand = brand

def drive(self):
print("Driving a vehicle.")

class Car(Vehicle): # Derived class inheriting from Vehicle
def honk(self):
print("Car honking.")

my_car = Car("Toyota")
my_car.drive() # Output: Driving a vehicle. (Inherited method from base class)
my_car.honk() # Output: Car honking. (Defined method in derived class)

Single Inheritance

In single inheritance, a derived class inherits attributes and methods from a single base class. The derived class can extend the functionality of the base class by adding new attributes and methods or overriding existing ones.

Single Inheritance
class Animal:
def speak(self):
print("Animal speaking.")

class Dog(Animal):
def speak(self):
print("Dog barking.")

my_dog = Dog()
my_dog.speak() # Output: Dog barking. (Overrides the speak() method of the base class)

Multiple Inheritance

Python supports multiple inheritance, allowing a derived class to inherit from multiple base classes. This enables the derived class to inherit attributes and methods from multiple sources.

Multiple Inheritance
class Flyer:
def fly(self):
print("Flying.")

class Swimmer:
def swim(self):
print("Swimming.")

class Duck(Flyer, Swimmer): # Derived class inheriting from multiple base classes
def quack(self):
print("Quack quack!")

my_duck = Duck()
my_duck.fly() # Output: Flying. (Inherited method from Flyer class)
my_duck.swim() # Output: Swimming. (Inherited method from Swimmer class)
my_duck.quack() # Output: Quack quack! (Defined method in derived class)

Method Resolution Order (MRO)

In cases of multiple inheritance, the order in which base classes are specified matters. Python uses the Method Resolution Order (MRO) to determine which method to execute when there are conflicts.

class A:
def hello(self):
print("Hello from A.")

class B(A):
def hello(self):
print("Hello from B.")

class C(A):
def hello(self):
print("Hello from C.")

class D(B, C):
pass

my_d = D()
my_d.hello() # Output: Hello from B. (MRO follows the order: D, B, C, A)

Conclusion

Inheritance is a fundamental concept in object-oriented programming that promotes code reuse, modularity, and the establishment of relationships between classes. By leveraging inheritance in Python, we can create specialized classes based on more generalized ones, inheriting their attributes and methods. Understanding inheritance empowers us to write efficient and maintainable code.

Happy coding and exploring the power of inheritance! Hope to see you soon…🚀🐍

Thank you for reading until the end. Please consider following the writer and this publication. Visit Stackademic to find out more about how we are democratizing free programming education around the world.

--

--

Rahul Pandey
Rahul Pandey

An experienced AI and ML Developer

Stackademic
Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.