Inheritance in Object Oriented Programming in Python

By inheritance we can use facilities of base class while adding more functionality to derived class . This helps in reusability of code and easy maintenance.

Base or parent class : Our main class where methods and properties are declared.
Derived or child class: Can use methods and properties of Base class along with its own functionality.

Example :

We have two class here, one is base or parent class animal() and another one is derived or child class wild(). We are creating an object tiger of class wild(). You can see the attributes name and height belongs to parent class animal() whereas weight belongs to child class wild().

Since wild() is a object of derived class wild(), we have used inheritance to use parent class attributes name and height along with the weight and displayed the values.
class animals():
    home='Jungle' #class attribute
#instance attributes
    def __init__(self,name,height):
        self.name= name
        self.height=height
class wild(animals):
    weight=2
    
tiger=wild('Tom',3)    # object is created. 
print(tiger.name, tiger.height,tiger.weight,tiger.home,sep=',')
Output
Tom,3,2,Jungle

Using Base class

We will create an object of base class animals()
lion=animals('Alex',5) # object 
print(lion.name) # Alex
print(lion.weight) # error 
The last line will generate error as weight is not an attribute of base class animal() ( it is attribute of child class wild() )

Child class with __init__()

Note that we have not used method __init__() in our child class. Since child class has inherited the __init__() method of parent class so we continue to use the same. We can also use __init__() in our child class. But this constructor of __init__() in child class will disconnect the inheritance of constructor from parent class. The last line print(tiger.height) in below code will generate error as we have not inherited the base __init__() method.
class animals():
    
#instance attributes
    def __init__(self,name,height):
        self.name= name
        self.height=height
    def legs(self):
        print("has four legs")
    
class wild(animals):
    def __init__(self,name,weight):
        self.name=name
        self.weight=weight
            
tiger=wild('Tom',3)	# object is created. 
print(tiger.weight)
print(tiger.height)	# Error 
To use the base class methods we will add this line animals.__init__(self,name,5) to our __init__() in child class.
class animals():
    home='Jungle' #class attribute
#instance attributes
    def __init__(self,name,height):
        self.name= name
        self.height=height
    def legs(self):
        print("has four legs")
    
class wild(animals):
    def __init__(self,name,weight):
        self.name=name
        self.weight=weight
        animals.__init__(self,name,5)
             
tiger=wild('Tom',3)    # object is created. 
print(tiger.weight) # 3
print(tiger.height) # 5

super()

In above code we have used the name of our base class animals() to inherit the constructor.
animals.__init__(self,name,5)
We need not explicitly declare the base class and just use super() function in python to use all the methods of base class. This is a big advantage while using multiple inheritance. Here is the example of linking base class by using super().
super().__init__(name,5)
The full code is here
class animals():
    home='Jungle' #class attribute
#instance attributes
    def __init__(self,name,height):
        self.name= name
        self.height=height
    def legs(self):
        print("has four legs")
    
class wild(animals):
    def __init__(self,name,weight):
        self.name=name
        self.weight=weight
        #animals.__init__(self,name,5)
        super().__init__(name,5)   
    
tiger=wild('Tom',3)    # object is created. 
print(tiger.weight) # 3
print(tiger.height) # 5   
tiger.legs()

Using isinstance()

By using isinstance() we can check if the object is instance of the class or not. Here we will check if the object tiger is instance of animals() or not. Use this line at the end of above code.
print(isinstance(tiger,animals)) # True

Object Oriented Programming in Python


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer