Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Inheritance

Inheritance

Inheritance is an Object-Oriented Programming (OOP) concept where one class (child class) derives properties and behaviors from another class (parent class). It helps in code reusability and avoids redundancy.

Types of Inheritance in Python

1.Single Inheritance

2. Multiple Inheritance

3. Multilevel Inheritance

4. Hierarchical Inheritance

5. Hybrid Inheritance

Single Inheritance

Single Inheritance means a child class inherits from a single parent class.

Example

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

    def show_brand(self):
        print(f"Brand: {self.brand}")

# Child class (inherits from Vehicle)
class Car(Vehicle):
    def __init__(self, brand, model):
        super().__init__(brand)  # Call parent class constructor
        self.model = model

    def show_details(self):
        print(f"Car: {self.brand} {self.model}")

# Creating an object of Car
car1 = Car("Toyota", "Corolla")
car1.show_brand()  # Calls parent class method
car1.show_details()  # Calls child class method

Output:

Brand: Toyota
Car: Toyota Corolla

Code Explanation

1. Defining the Parent Class (Vehicle)

class Vehicle:
    def __init__(self, brand):
        self.brand = brand  # Attribute (stores brand name)

    def show_brand(self):
        print(f"Brand: {self.brand}")
  • class Vehicle defines a parent class (Vehicle).
  • Constructor (__init__) initializes the brand attribute.
  • Method (show_brand) prints the brand name.

2. Defining the Child Class (Car)

class Car(Vehicle):
    def __init__(self, brand, model):
        super().__init__(brand)  # Call parent class constructor
        self.model = model  # New attribute for child class

    def show_details(self):
        print(f"Car: {self.brand} {self.model}")
  • class Car(Vehicle)Car inherits from Vehicle.
  • super().__init__(brand) calls the Vehicle constructor to initialize brand.
  • New attribute (model) is added in Car.
  • Method (show_details) prints both the brand and model.

3. Creating an Object of Car

car1 = Car("Toyota", "Corolla")
  • Creates an instance of Car (car1).
  • Calls __init__() in Car, which calls super().__init__(brand) to initialize brand.

4. Calling Methods

car1.show_brand()  # Calls method from Parent class
car1.show_details()  # Calls method from Child class
  • Method Calls Breakdown
  1. car1.show_brand()
    • Looks for show_brand() in Car class.
    • Not found, so it checks the Vehicle class.
    • Prints Brand: Toyota.
  2. car1.show_details()
    • Found in Car class.
    • Prints Car: Toyota Corolla.

Multiple Inheritance

A child class inherits from more than one parent class.

Example

# Parent class 1
class Engine:
    def engine_type(self):
        print("This car has a V8 engine.")

# Parent class 2
class Wheels:
    def wheel_type(self):
        print("This car has alloy wheels.")

# Child class (inherits from both Engine and Wheels)
class SportsCar(Engine, Wheels):
    def car_type(self):
        print("This is a sports car.")

# Creating an object
car = SportsCar()
car.car_type()    # Calls method from SportsCar
car.engine_type() # Calls method from Engine
car.wheel_type()  # Calls method from Wheels

Output:

This is a sports car.
This car has a V8 engine.
This car has alloy wheels.

  • SportsCar inherits from both Engine and Wheels.
  • The child class can access methods from both parent classes.

Multilevel Inheritance

A child class inherits from another child class.

Example

# Grandparent class
class Animal:
    def breathe(self):
        print("This animal breathes.")

# Parent class (inherits from Animal)
class Mammal(Animal):
    def has_fur(self):
        print("This mammal has fur.")

# Child class (inherits from Mammal)
class Dog(Mammal):
    def bark(self):
        print("Dog barks: Woof! Woof!")

# Creating an object of Dog
dog = Dog()
dog.breathe()  # Inherited from Animal
dog.has_fur()  # Inherited from Mammal
dog.bark()     # Method from Dog class

Output:

This animal breathes.
This mammal has fur.
Dog barks: Woof! Woof!

🔹 Dog class inherits from Mammal, which inherits from Animal.

🔹 The Dog class can use methods from both parent and grandparent classes.

Hierarchical Inheritance

A single parent class has multiple child classes.

Example

# Parent class
class Vehicle:
    def show(self):
        print("This is a vehicle.")

# Child class 1
class Car(Vehicle):
    def car_type(self):
        print("This is a car.")

# Child class 2
class Bike(Vehicle):
    def bike_type(self):
        print("This is a bike.")

# Creating objects
car = Car()
bike = Bike()

car.show()  # Calls parent class method
car.car_type()  # Calls Car class method

bike.show()  # Calls parent class method
bike.bike_type()  # Calls Bike class method

Output:

This is a vehicle.
This is a car.
This is a vehicle.
This is a bike.

🔹 Both Car and Bike classes inherit from Vehicle.
🔹 Each child class has its own unique method.

Hybrid Inheritance

A mix of different inheritance types.

Example

class A:
    def method_A(self):
        print("Method of Class A")

class B(A):  # Single Inheritance
    def method_B(self):
        print("Method of Class B")

class C(A):  # Hierarchical Inheritance
    def method_C(self):
        print("Method of Class C")

class D(B, C):  # Multiple Inheritance
    def method_D(self):
        print("Method of Class D")

# Creating an object of Class D
obj = D()
obj.method_A()  # Inherited from A
obj.method_B()  # Inherited from B
obj.method_C()  # Inherited from C
obj.method_D()  # Defined in Class D

Output:

Method of Class A
Method of Class B
Method of Class C
Method of Class D

🔹 D inherits from both B and C, which inherit from A.
🔹 This is a mix of single, hierarchical, and multiple inheritance.

    Leave a Reply

    Your email address will not be published.

    Need Help?