Thursday, 6 March 2025

Inheritance in Python

Inheritance is an object-oriented programming (OOP) concept that allows a class (child class) to inherit attributes and methods from another class (parent class). It promotes code reusability and establishes a relationship between classes.


Why Use Inheritance?

  1. Code Reusability: Avoids redundancy by reusing existing code.
  2. Maintainability: Centralizes common code in a base class.
  3. Extensibility: Enables modifying or extending functionality without altering existing code.
  4. Polymorphism Support: Allows for method overriding and dynamic method resolution.

Types of Inheritance in Python

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

1. Single Inheritance

A child class inherits from a single parent class.


# Parent class class Animal: def speak(self): print("Animal makes a sound") # Child class class Dog(Animal): def bark(self): print("Dog barks") # Creating an object of Dog class dog = Dog() dog.speak() # Output: Animal makes a sound dog.bark() # Output: Dog barks

Key Points:

  • Simplest form of inheritance.
  • The child class has access to the parent class methods.

2. Multiple Inheritance

A child class inherits from more than one parent class.


# Parent class 1 class Father: def height(self): print("Height from Father") # Parent class 2 class Mother: def color(self): print("Color from Mother") # Child class class Child(Father, Mother): def display(self): print("Child's unique method") # Creating an object of Child class child = Child() child.height() # Output: Height from Father child.color() # Output: Color from Mother child.display() # Output: Child's unique method

Key Points:

  • Can inherit features from multiple classes.
  • Method Resolution Order (MRO) defines the method search order.

3. Multilevel Inheritance

A child class inherits from a parent class, which in turn inherits from another parent class.


# Grandparent class class Animal: def speak(self): print("Animal speaks") # Parent class class Dog(Animal): def bark(self): print("Dog barks") # Child class class Puppy(Dog): def weep(self): print("Puppy weeps") # Creating an object of Puppy class puppy = Puppy() puppy.speak() # Output: Animal speaks puppy.bark() # Output: Dog barks puppy.weep() # Output: Puppy weeps

Key Points:

  • Establishes a hierarchical relationship.
  • The child class has access to all ancestor methods.

4. Hierarchical Inheritance

Multiple child classes inherit from a single parent class.


# Parent class class Vehicle: def engine(self): print("Engine of Vehicle") # Child class 1 class Car(Vehicle): def wheels(self): print("Car has 4 wheels") # Child class 2 class Bike(Vehicle): def wheels(self): print("Bike has 2 wheels") # Creating objects of child classes car = Car() car.engine() # Output: Engine of Vehicle car.wheels() # Output: Car has 4 wheels bike = Bike() bike.engine() # Output: Engine of Vehicle bike.wheels() # Output: Bike has 2 wheels

Key Points:

  • Useful when multiple classes share common functionality.
  • Provides code reuse for common behavior.

5. Hybrid Inheritance

A combination of two or more types of inheritance.


class Animal: def speak(self): print("Animal speaks") class Mammal(Animal): def walk(self): print("Mammal walks") class Bird(Animal): def fly(self): print("Bird flies") class Bat(Mammal, Bird): def nocturnal(self): print("Bat is nocturnal") # Creating an object of Bat class bat = Bat() bat.speak() # Output: Animal speaks bat.walk() # Output: Mammal walks bat.fly() # Output: Bird flies bat.nocturnal() # Output: Bat is nocturnal

Key Points:

  • Can lead to complex structures.
  • Requires understanding of Method Resolution Order (MRO).

Method Overriding

A child class can override a method of the parent class.


class Animal: def sound(self): print("Animal makes a sound") class Dog(Animal): def sound(self): print("Dog barks") # Creating an object of Dog class dog = Dog() dog.sound() # Output: Dog barks

Key Points:

  • Enhances or modifies the parent class method.
  • Useful in polymorphism.

The super() Function

The super() function allows you to call methods of the parent class.


class Animal: def sound(self): print("Animal makes a sound") class Dog(Animal): def sound(self): super().sound() # Calls the parent class method print("Dog barks") # Creating an object of Dog class dog = Dog() dog.sound()

Output:

Animal makes a sound Dog barks

Key Points:

  • Useful in method overriding.
  • Avoids the need to hardcode the parent class name.

The isinstance() and issubclass() Functions

isinstance(object, class)

Checks if an object is an instance of a class (or a subclass).


class Animal: pass class Dog(Animal): pass dog = Dog() print(isinstance(dog, Dog)) # Output: True print(isinstance(dog, Animal)) # Output: True

issubclass(subclass, superclass)

Checks if a class is a subclass of another class.


print(issubclass(Dog, Animal)) # Output: True print(issubclass(Animal, Dog)) # Output: False

Best Practices for Inheritance

  1. Avoid deep inheritance chains: Prefer composition over inheritance when possible.
  2. Use super() to call parent class methods.
  3. Keep the inheritance hierarchy simple and readable.
  4. Apply method overriding only when necessary.
  5. Understand the Method Resolution Order (MRO), especially in multiple inheritance.

When to Use Inheritance:

  • When you need to model relationships like "is-a" (e.g., a Dog is an Animal).
  • To reuse code across similar classes.
  • When you need to extend functionality of existing classes.

Conclusion

Inheritance is a powerful tool in OOP that promotes code reuse, enhances maintainability, and simplifies complex systems. However, it is crucial to use inheritance wisely and avoid overcomplicating class hierarchies.

No comments:

Post a Comment

Desktop Virtualisation

Desktop Virtualization ( DV ) Desktop Virtualization ( DV ) is a technique that creates an illusion of a desktop provided to the user. It d...