Inheritance

 

Inheritance

Implementing Inheritance

Inheritance provides re-usability. With use of inheritance one class acquire the features of another class.

The existing class from where the class is inherited is called parent class or base class or super class. The class which inherits the child class is called the derived class or child class or sub class.

Example of inheritance:

Create a file vehicle.py:

class vehicle:

def setbrand(self,brand):

self.brand=brand

def getbrand(self):

return self.brand

Create a file fourwheeler.py:

from vehicle import vehicle


class fourwheeler(vehicle):

def setcapacity(self,capacity):

self.capacity=capacity

def getcapacity(self):

return self.capacity

car=fourwheeler()

car.setbrand("hundai")

car.setcapacity("1200cc")

print("The car brand is" + car.getbrand())

print("The car capacity is" +car.getcapacity())

Output:

The car brand ishundai

The car capacity is1200cc


Constructor in inheritance

The constructor of base class is by default available to sub class.

Example:

'''program of inheritance base class constructor from derived class'''

class base:

def __init__(self):

self.value=100

def display_val(self):

print("Value is", self.value)


class derived(base):

pass


d1=derived()

d1.display_val()

output:

('Value is', 100)

Overriding super class constructors and Methods

When method is written in sub class the super class method is not available in sub class. it is called method overriding. In constructor implicitly super keyword is used for default constructor in sub class to call default constructor of super class.

Example:

'''program of inheritance

constructor overriding

method overriding '''

class base:

def __init__(self):

self.value=100

def display_val(self):

self.value=5

print("Value is", self.value)


class derived(base):

def __init__(self):

self.value1=200

def display_val1(self):

print("Value1 is",self.value1)

d1=derived()

d1.display_val()

d1.display_val1()

Output:

('Value is', 5)

('Value1 is', 200)

Through above example we can identify that the default constructor is invoked automatically. If we make the derive class method name

display_val()

instead of

display_val1() then base class method can not be accessed. This is called method overriding.

Example:

class base:

def __init__(self):

self.value=100

def display_val(self):

self.value=5

print("Value is", self.value)


class derived(base):

def __init__(self):

self.value1=200

def display_val(self):

print("Value1 is",self.value1)

d1=derived()

d1.display_val()

#d1.display_val1()

Output:

Value1 is 200


The super method

In python super() is inbuilt method that is used to call the super class method or constructors.

To initialize the instance variable of super class through sub class super() method is used.

Syntax:

super().__init__()

The above syntax is used to call the super class default constructor.

super().__init__(parameters)

The above syntax is used to call the super class parameterized constructor. With use of above syntax value can be passed to the super class constructor from sub class constructor.

super().method()

The above syntax is used to call the super class methods.

'''program of inheritance

super constructor '''

class base:

def __init__(self,v):

self.value=v

def display_val(self):

print("Value is", self.value)

class derived(base):

def __init__(self,v,v1):

self.value1=v1

super().__init__(v)

def display_val1(self):

print("Value1 is",self.value1)

d1=derived(5,10)

d1.display_val()

d1.display_val1()

Output:

Value is 5

Value1 is 10

Example:

'''program of inheritance

super method '''

class base:

def __init__(self,v):

self.value=v

def display_val(self):

print("Value in base is ", self.value)

class derived(base):

def __init__(self,v,v1):

self.value1=v1

super().__init__(v)

def display_val(self):

print("Value in derived is",self.value1)

super().display_val()

d1=derived(5,10)

d1.display_val()

Output:

Value in derived is 10

Value in base is 5