python

Inheritance


Inheritance is one of the key features of object-oriented programming (OOP). It allows one class (child class) to inherit attributes and methods from another class (parent class). This promotes code reuse and can make your programs more modular and easier to maintain.

 

Basic Concepts

  1. Parent Class (Base Class or Superclass): The class whose attributes and methods are inherited.
  2. Child Class (Derived Class or Subclass): The class that inherits attributes and methods from the parent class.

 

 

Types of Inheritance

  1. Single Inheritance: A child class inherits from one parent class.
  2. Multiple Inheritance: A child class inherits from more than one parent class.
  3. Multilevel Inheritance: A child class inherits from a parent class, which in turn inherits from another parent class.
  4. Hierarchical Inheritance: Multiple child classes inherit from one parent class.
  5. Hybrid Inheritance: A combination of two or more types of inheritance

 

 

Syntax and Examples

 

1. Single Inheritance

In single inheritance, a child class inherits from a single parent class.

 

Example:

# Parent class
class Animal:
   def __init__(self, name):
       self.name = name
   def speak(self):
       raise NotImplementedError("Subclass must implement abstract method")
# Child class
class Dog(Animal):
   def speak(self):
       return f"{self.name} says Woof!"
# Creating an instance of Dog
dog = Dog("Buddy")
print(dog.speak())  # Output: Buddy says Woof!
 

 

2. Multiple Inheritance

In multiple inheritance, a child class inherits from more than one parent class.

 

Example:

# Parent class 1
class Walker:
   def walk(self):
       return "Walking"
# Parent class 2
class Swimmer:
   def swim(self):
       return "Swimming"
# Child class
class Amphibian(Walker, Swimmer):
   pass
# Creating an instance of Amphibian
amphibian = Amphibian()
print(amphibian.walk())  # Output: Walking
print(amphibian.swim())  # Output: Swimming
 

 

3. Multilevel Inheritance

In multilevel inheritance, a child class inherits from a parent class, which in turn inherits from another parent class.

 

Example:

# Parent class
class Animal:
   def __init__(self, name):
       self.name = name
   def speak(self):
       raise NotImplementedError("Subclass must implement abstract method")
# Intermediate class
class Mammal(Animal):
   def feed_young(self):
       return f"{self.name} is feeding its young"
# Child class
class Dog(Mammal):
   def speak(self):
       return f"{self.name} says Woof!"
# Creating an instance of Dog
dog = Dog("Buddy")
print(dog.speak())        # Output: Buddy says Woof!
print(dog.feed_young())   # Output: Buddy is feeding its young
 

 

4. Hierarchical Inheritance

In hierarchical inheritance, multiple child classes inherit from a single parent class.

 

Example:

# Parent class
class Animal:
   def __init__(self, name):
       self.name = name
   def speak(self):
       raise NotImplementedError("Subclass must implement abstract method")
# Child class 1
class Dog(Animal):
   def speak(self):
       return f"{self.name} says Woof!"
# Child class 2
class Cat(Animal):
   def speak(self):
       return f"{self.name} says Meow!"
# Creating instances of Dog and Cat
dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.speak())  # Output: Buddy says Woof!
print(cat.speak())  # Output: Whiskers says Meow!
 

 

5. Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance. It can involve any combination of single, multiple, multilevel, and hierarchical inheritance.

 

Example:

# Parent class
class Animal:
   def __init__(self, name):
       self.name = name
   def speak(self):
       raise NotImplementedError("Subclass must implement abstract method")
# Intermediate class 1
class Walker(Animal):
   def walk(self):
       return "Walking"
# Intermediate class 2
class Swimmer(Animal):
   def swim(self):
       return "Swimming"
# Child class
class Amphibian(Walker, Swimmer):
   def speak(self):
       return f"{self.name} can walk and swim"
# Creating an instance of Amphibian
amphibian = Amphibian("Froggy")
print(amphibian.speak())  # Output: Froggy can walk and swim
print(amphibian.walk())   # Output: Walking
print(amphibian.swim())   # Output: Swimming
 

 

Overriding Methods

A child class can override or extend the functionality of methods inherited from a parent class.

Example:

class Animal:
   def speak(self):
       return "Animal sound"
class Dog(Animal):
   def speak(self):
       return "Woof!"
# Creating instances
animal = Animal()
dog = Dog()
print(animal.speak())  # Output: Animal sound
print(dog.speak())     # Output: Woof!
 

 


python