Python Inheritance

Inheritance is an important concept in OOP, Inheritance refers to acquiring attributes and methods from one class to another. The class from which the attributes and behavior is inherited is termed as parent class or base class. The class which inherits the attribute and methods is termed as child class or derived class. 

Inheritance is performed by passing one class name to another. This is done by adding round brackets in the child class with parent class name. Let’s see an example

class Parent:
    a = "Parent attribute"

class Base(Parent):

    def display(self):
        print(self.a)

base_object = Base()
base_object.display()

Output:

Parent attribute

Here you have created a Parent class which is the base class. The Base class is the child class as it inherits from parent class. Once the inheritance is performed the variable a in parent class becomes available to Base class.

Multiple inheritance

You can inherit from multiple classes and have their properties in a child class. For instance.

class Animal:
    has_legs = "Has legs"


class Dog:
    number_of_legs = 4

    def bark(self):
        print('Dog is barking')


class PamerianDog(Animal, Dog):

    def breed(self):
        print("Its a Pamarian")


p = PamerianDog()
print(p.has_legs)
print(p.number_of_legs)
p.bark()
p.breed()

Output:

Has legs
4
Dog is barking
Its a Pamarian

Multi-Level Inheritance

Multi- level inheritance refers to inheritance through multiple class. Let’s see an example

class Animal:
    has_legs = "Has legs"


class Dog(Animal):
    number_of_legs = 4

    def bark(self):
        print('Dog is barking')


class PamerianDog(Dog):

    def breed(self):
        print("Its a Pamarian")


p = PamerianDog()
print(p.has_legs)
print(p.number_of_legs)
p.bark()
p.breed()

Output:

Has legs
4
Dog is barking
Its a Pamarian

Function overriding

If we have same function in base class with the same signature then function in parent class is overridden by base class. Let’s see an example

class Animal:

    def make_sound(self):
        print('No sound')


class Dog(Animal):

    def make_sound(self):
        print('Dog is barking')

obj = Dog()
obj.make_sound()

Output:

Dog is barking

Here the parent make_sound() method is overridden in the child class. 

Function overloading

Function overloading is an important concept in most of the other OOP language. It means having same function name with different parameter signature. However , python does not support function overloading.

super() method 

super() method is special method in python. It is used to invoke methods and parameters in base class. It is particularly useful for calling methods that have been overridden in base class. Let’s see an example,

class Person:
    def __init__(self, fname, lname):
        self.first_name = fname
        self.last_name = lname

class Student(Person):
    def __init__(self, fn, ln):
        super().__init__(fn, ln)
        self.major = "Computer science"

    def student_info(self):
        print(self.first_name,'', self.last_name, ' Major:', self.major )

s = Student('Bill', 'Gates')
s.student_info()

Output:

Bill  Gates  Major: Computer science

Here as you can see __init__() method is overridden, however, super() makes it easy to access the parent method.

Leave a Reply

Your email address will not be published. Required fields are marked *