Python Inheritance


Inheritance in Python OOP

Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class (known as a subclass or child class) to inherit attributes and methods from another class (known as a superclass or parent class). This promotes code reusability, reduces redundancy, and allows for the creation of a hierarchical class structure.

Key Concepts of Inheritance

  1. Base Class (Parent Class): The class whose properties and methods are inherited.
  2. Derived Class (Child Class): The class that inherits from the base class. It can add new properties and methods or override existing ones.
  3. Method Overriding: The ability of a child class to provide a specific implementation of a method that is already defined in its parent class.
  4. Multiple Inheritance: A feature that allows a class to inherit from multiple parent classes.

Basic Inheritance

Here’s a simple example of inheritance where a child class inherits from a parent class:

Example of Basic Inheritance

# Parent class class Animal: def __init__(self, name): self.name = name def speak(self): # Method in the parent class return "Animal speaks" # Child class inheriting from Animal class Dog(Animal): def speak(self): # Overriding the parent class method return f"{self.name} says Woof!" # Creating an instance of the Dog class dog = Dog("Buddy") # Calling methods print(dog.name) # Output: Buddy (inherited from Animal) print(dog.speak()) # Output: Buddy says Woof! (overridden method)

Multiple Inheritance

In Python, a class can inherit from multiple classes, which is known as multiple inheritance. This allows a derived class to combine attributes and methods from multiple base classes.

Example of Multiple Inheritance

# Base class 1 class Canine: def bark(self): return "Bark!" # Base class 2 class Pet: def play(self): return "Playing!" # Child class inheriting from both Canine and Pet class Dog(Canine, Pet): def fetch(self): return "Fetching the ball!" # Creating an instance of the Dog class dog = Dog() # Calling methods print(dog.bark()) # Output: Bark! print(dog.play()) # Output: Playing! print(dog.fetch()) # Output: Fetching the ball!

Using super()

The super() function allows you to call methods from a parent class within a child class. This is particularly useful when overriding methods, as it lets you extend the functionality of the parent method rather than completely replacing it.

Example of Using super()

class Animal: def __init__(self, name): self.name = name def speak(self): return "Animal speaks" class Dog(Animal): def __init__(self, name, breed): super().__init__(name) # Calling the constructor of the parent class self.breed = breed def speak(self): # Overriding the speak method return f"{self.name} the {self.breed} says Woof!" # Creating an instance of Dog dog = Dog("Buddy", "Golden Retriever") # Calling methods print(dog.speak()) # Output: Buddy the Golden Retriever says Woof!

Summary

  • Inheritance: A mechanism that allows one class to inherit attributes and methods from another class.
  • Base Class: The parent class from which properties and methods are inherited.
  • Derived Class: The child class that inherits from the base class and can override or extend its functionality.
  • Method Overriding: The ability of a child class to provide a specific implementation of a method defined in the parent class.
  • Multiple Inheritance: The ability of a class to inherit from multiple parent classes.
  • super() Function: A built-in function used to call methods from the parent class, allowing for easier method overriding and access to parent class properties.

Inheritance is a powerful feature in OOP that promotes code reuse and establishes a natural hierarchy between classes, making it easier to manage complex software systems!