Understanding Types of Inheritance in Python with Examples

 Understanding Types of Inheritance in Python with Examples
Understanding Types of Inheritance in Python with Examples
 Understanding Types of Inheritance in Python with Examples


 Understanding Types of Inheritance in Python with Examples

  • Inheritance is a fundamental concept in object-oriented programming that allows a class to inherit properties and behaviours from another class. 
  • Python supports several types of inheritance, each serving different purposes. 
  • In this article, we'll explore the various types of inheritance in Python and provide examples to illustrate each.

1. Single Inheritance

  • Single inheritance involves a class inheriting from only one base class. 
  • It's the simplest form of inheritance in Python.

class Animal:

    def speak(self):

        print("Animal speaks")

class Dog(Animal):

    def bark(self):

        print("Dog barks")


# Creating an instance of the Dog class

my_dog = Dog()


# Accessing methods from both Animal and Dog classes

my_dog.speak()  # Output: Animal speaks

my_dog.bark()   # Output: Dog barks

  • In this example, the `Dog` class inherits from the `Animal` class.

2. Multiple Inheritance

  • Multiple inheritance allows a class to inherit from more than one base class.
  • It enables a derived class to inherit properties and behaviours from multiple parent classes.

class Flyable:

    def fly(self):

        print("Can fly")

class Swimmable:

    def swim(self):

        print("Can swim")

class Duck(Flyable, Swimmable):

    def quack(self):

        print("Duck quacks")

# Creating an instance of the Duck class

my_duck = Duck()

# Accessing methods from Flyable, Swimmable, and Duck classes

my_duck.fly()   # Output: Can fly

my_duck.swim()  # Output: Can swim

my_duck.quack() # Output: Duck quacks

  • Here, the `Duck` class inherits from both the `Flyable` and `Swimmable` classes.

3. Multilevel Inheritance

  • In multilevel inheritance, a class inherits from another class, and then another class inherits from it. 
  • It forms a chain of inheritance.

class Shape:

    def draw(self):

        print("Drawing shape")

class Circle(Shape):

    def draw_circle(self):

        print("Drawing circle")

class ColoredCircle(Circle):

    def set_color(self, color):

        print(f"Setting color to {color}")

# Creating an instance of the ColoredCircle class

my_colored_circle = ColoredCircle()

# Accessing methods from Shape, Circle, and ColoredCircle classes

my_colored_circle.draw()        # Output: Drawing shape

my_colored_circle.draw_circle() # Output: Drawing circle

my_colored_circle.set_color("red")  # Output: Setting color to red

  • In this example, `ColoredCircle` inherits from `Circle`, which in turn inherits from `Shape`.

4. Hierarchical Inheritance

  • In hierarchical inheritance, multiple classes inherit from a single base class.

class Vehicle:

    def move(self):

        print("Vehicle moves")

class Car(Vehicle):

    def honk(self):

        print("Car honks")

class Bike(Vehicle):

    def ring_bell(self):

        print("Bike rings bell")

# Creating instances of the Car and Bike classes

my_car = Car()

my_bike = Bike()

# Accessing methods from Vehicle, Car, and Bike classes

my_car.move()        # Output: Vehicle moves

my_car.honk()        # Output: Car honks

my_bike.move()       # Output: Vehicle moves

my_bike.ring_bell()  # Output: Bike rings bell

  • Here, both `Car` and `Bike` classes inherit from the `Vehicle` class.

5. Hybrid Inheritance

  • Hybrid inheritance is a combination of two or more types of inheritance within a single program. 
  • It often involves the use of multiple, multilevel, or hierarchical inheritance in various combinations.

class A:

    def method_a(self):

        print("This is Class A")

class B(A):

    def method_b(self):

        print("This is cass B")

class C(A):

    def method_c(self):

        print("This is class C")

class D(B, C):

    def method_d(self):

        print("This is Class D")

# Creating an instance of the D class

my_instance_d = D()

# Accessing methods from A, B, C, and D classes

my_instance_d.method_a()  # Output: This is Class A

my_instance_d.method_b()  # Output: This is Class B

my_instance_d.method_c()  # Output: This is Class C

my_instance_d.method_d()  # Output: This is Class D

  • Here, class `D` inherits from both classes `B` and `C`, forming a hybrid inheritance structure.

6. Method Resolution Order (MRO)

  • Method Resolution Order is the order in which base classes are searched when a method is called in a derived class. 
  • It plays a crucial role in multiple and hybrid inheritance.

class X:

    def show(self):

        print("This is Class X")

class Y(X):

    def show(self):

        print("This is Class Y")

class Z(X):

    def show(self):

        print("This is Class Z")

class XY(Z, Y):

    pass

# Creating an instance of the XY class

my_instance_xy = XY()

# Accessing the show method from the XY class

my_instance_xy.show()  # Output: This is Class Z

  • In this example, the `XY` class inherits from both `Z` and `Y` classes. 
  • The Method Resolution Order is determined by the C3 linearization algorithm, and it prioritizes the leftmost parent (`Z` in this case).

  • Understanding MRO is important for predicting the behavior of method calls in complex inheritance scenarios.

Conclusion

  • Python provides a rich set of options for implementing inheritance, allowing developers to design flexible and reusable code. 
  • The choice of inheritance type depends on the specific requirements of your application. Whether you need a simple single inheritance or a more complex combination of multiple types, Python's versatile inheritance system has you covered

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.