Inheritance In Python

What is Inheritance?

Inheritance is the capability of one class to acquire the common state and behavior of another class while adding its own functionality . In Object-Oriented programming , the concept of inheritance provides the idea of re-usability.This means we can add additional features to a class without modifying it.

Inheritance In Python

In Python, inheritance is a mechanism that allows a class to inherit properties and methods from another class. This promotes code reuse and supports the concept of hierarchical classification.

Here’s a basic example of inheritance in Python:

class Animal:
    def __init__(self, species):
        self.species = species

    def make_sound(self):
        return "Default..."


class Cat(Animal):
    # child class inherits the parent's __init__() function.

    #  Overriding method of Animal class. 
    def make_sound(self):
        return "Meow!!!"


class Dog(Animal):
    # # child class inherits the parent's __init__() function.

    #  Overriding method of Animal class. 
    def make_sound(self):
        return "Woof!!!"


dog = Dog("Jackie")
print("Dog Specie: ", dog.species)
print("Dog Sound", dog.make_sound())

cat = Cat("Tuna")
print("Cat Specie: ", cat.species)
print("Cat Sound: ", cat.make_sound())

# Output
Dog Specie:  Jackie
Dog Sound Woof!!!
Cat Specie:  Tuna
Cat Sound:  Meow!!!

In this example:

  • We have a base class Animal with a constructor __init__ that takes a species parameter and a method make_sound that’s meant to be overridden.
  • We then create two subclasses, Dog and Cat, which inherit from Animal. These subclasses override the make_sound method.
  • When you create an instance of Dog or Cat, you can access both the properties and methods of the parent class, Animal, as well as the methods overridden in the subclass.

Inheritance in Python follows the syntax class SubClassName(BaseClassName). This indicates that SubClassName inherits from BaseClassName.

Adding __init__() Function to child class

When you add an __init__() function to a child class in Python, you’re essentially overriding the __init__() method of the parent class. This allows you to customize the initialization process for instances of the child class while still potentially utilizing the initialization logic from the parent class.

Here’s an example to illustrate adding an __init__() function to a child class:

class Employee:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def show_details(self):
        print(self.firstname, self.lastname)


class Developer(Employee):
    def __init__(self, fname, lname, dept):
        Employee.__init__(self, fname, lname)
        self.dept = dept


x = Developer("Shivaay", "Chauhan", "Developer")
x.show_details()
print(x.dept)

# Output
Shivaay Chauhan
Developer

The child’s __init__() function overrides the inheritance of the parent’s __init__() function. To keep the inheritance of the parent’s __init__() function, add a call to the parent’s __init__() function.

Adding super() function in child class

You can define a super() function that will make the child class inherit all the methods and properties from its parent:

class Employee:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def show_details(self):
        print(self.firstname, self.lastname)


class Developer(Employee):
    def __init__(self, fname, lname, dept):
        # Using Super() function.
        super().__init__(fname, lname)
        self.dept = dept


x = Developer("Shivaay", "Chauhan", "Developer")
x.show_details()
print(x.dept)

# Output
Shivaay Chauhan
Developer

Leave a Reply