Tuesday, November 20, 2018

Python Ninja Bootcamp 27-Inheritance

Learn Python like a Professional! Take You from 0 to Hero




In this Python lecture, we will learn about the Inheritance.

"Inheritance is the process of creating by using existing class".

The new class is called Derived / Sub / Child class.

The existing class is called Base / Super / Parent class.
  


Let's see the example of Inheritance-


class A:
    def __init__(self):
        print('Class A init method')
 
class B(A):
    pass

Here A is base class and B is the derived class. B inherits of all the feature of A


ob1 = B()
>> Class A init method

class A:
    def __init__(self):
        print('Class A init method')
 
class B(A):
    def __init__(self):
        print('Class B init method')     



ob1 = B()
>> Class B init method



When object of derived class is created, it will first try to find the init method in derived class.
If not found, it will call the init method of base class

Now Super function is used to call the method of base class

class A:
    def __init__(self):
        print('Class A init method')  
 
class B(A):
    def __init__(self):
        super().__init__()
        print('Class B init method')  


ob1 = B()
>> Class A init method
     Class B init method


Now let's create the display method base class A

class A:
    def __init__(self):
        print('Class A init method')
        
    def display(self):
        print('Class A display method')
 
class B(A):
    def __init__(self):
        super().__init__()
        print('Class B init method')   


ob1 = B()
>> Class A init method
     Class B init method

ob1.display()
>>Class A display method


Now let's create the display method for the derived class B


class A:
    def __init__(self):
        print('Class A init method')
        
    def display(self):
        print('Class A display method')
 
class B(A):
    def __init__(self):
        super().__init__()
        print('Class B init method') 
        
    def display(self):
        print('Class B display method')    


ob1 = B()
>> Class A init method
     Class B init method

ob1.display()
>>Class B display method


Now Use Super function to print the display method of  base class A


class A:
    def __init__(self):
        print('Class A init method')
        
    def display(self):
        print('Class A display method')
 
class B(A):
    def __init__(self):
        super().__init__()
        print('Class B init method') 
        
    def display(self):
        super().display()
        print('Class B display method')    


ob1 = B()
>> Class A init method
     Class B init method

ob1.display()
>> Class A display method
    Class B display method
Let's one more example of inheritance

class Student:
    
    def __init__(self,name,rollno,percentage):
        self.name= name
        self.rollno= rollno
        self.percentage= percentage
        
        
    def result(self):
        if self.percentage >30:
            print('Pass')
        else:
            print('fail')
            
class Branch(Student):
    
    def __init__(self,name,rollno,percentage,branch):
        super().__init__(name,rollno,percentage)
        self.branch = branch

ob2 = Branch('Tamta',1,90,'Cse')
print(ob2.name)
print(ob2.rollno)
print(ob2.percentage)
print(ob2.branch)
>> Tamta
      1
      90
     Cse

ob2.result()
>> Pass


EDITS ARE WELCOMED!!

In the next Blog, we will discuss Q&A 4th 

https://sngurukuls247.blogspot.com/2018/11/python-ninja-bootcamp-28-q-4th.html

......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnw

Instagram
https://www.instagram.com/python.india/

View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 



Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment