Inheritance in python tutorials point


Inheritance in python tutorials point :

Welcome everyone, today we will learn what is inheritance in python tutorials point. Inheritance is a very powerful feature in oop. It is represents real-world relationship and support transitivity.
Inheritance refers to defined a new class with no modification or small change to an existing class. The new class in inheritance is called derived class and the one from main class which called the base class.

Type of Inheritance in python :

Inheritance in python tutorials point
Inheritance in python tutorials point

Inheritance is basically categorized into 5 types −
  1. Single
  2. Multiple
  3. Hierarchical
  4. Multi-level
  5. Hybrid
 In inheritance we access the feature of other class without making the object of the parents class.
Python Inheritance Syntax


class BaseClass:


  # (write the Body of base class)


class DerivedClass(BaseClass):


  # (write the Body of derived class)


Above code shows the syntax for python inheritance
A main class can inherit multiples class by adding of them inside the bracket. 
check the following syntax.
Syntax


class derive-class(<main class 1>, <main class 2>,<main class 3> ..... <main class n>):  


    <class -inside>  



Example of Inheritance in Python
To see the use of inheritance, let us take an example on inheritance.
Example 1


class Bird:  


    def singing(self):  


        print("Bird Singing")  


#child class Crow inherits the base class Bird 


class crow(Bird:  


    def singing(self):  


        print("crow singing")  


c = crow()  


c.singing()  


c.singing()


Output:


crow singing


Bird singing


#1 single Inheritance in python 

Example


# (first use parent class)


class Students():


   # (constructor of parent class in this example)


   def __init__(my, name, Rollno.):


      my.name = name


      my.Rollno. = Rollno.


   def display(my):


      print(my.name)


      print(my.Rollno.)


# (use child class)


class College( Students):


   def __init__(my, name, Rollno., adsyear, branchname):


      my.adsyear = adsyear


      my.branchname = branch


      # invoking the __init__ of the parent class


      Student.__init__(my, name, Rollno.)


obj = College('Sachin',12345678,2017,"ME")


obj.display()


Output


Sachin


12345678


#2 Multiple inheritance in python 

Example


# (First use parent class)


class Students():


   # (constructor of parent class in this example)


   def __init__(my, name, Rollno.):


      my.name = name


      my.Rollno. = Rollno.


   def display(my):      print(my.name)      print(my.Rollno.)# child class#1


class school( Students ):


   def __init__(my, name, Rollno., admnyear, branch):      my.admnyear = admnyear      my.branch = branch      # invoking the __init__ of the parent class


      Student.__init__(my, name, Rollno.)# child class#2


class college( Students ):


   def __init__(my, name, Rollno., refno, branch):      my.refno = refno      my.branch = branch      # (init. of the parents class)


      Students.__init__(my, name, Rollno.)# creation of an object for class


obj_1 = College('Sachin',12345678,2017,"ME")


obj_1.display()


obj_2 = University ('Sachin',12345678,"2018","ME")


obj_2.display()


Output


Sachin


12345678


Sachin


12345678


Summary:
In this article, we saw the information about Inheritance in Python specially single and Multiple inheritance.
Some FAQ about Inheritance in python tutorials point :
Type of Inheritance in python ?
  1. Single
  2. Multiple
  3. Hierarchical
  4. Multi-level
  5. Hybrid
single Inheritance in python ?
In this Article we covered simple concept about single inheritance
 Multiple inheritance in python ?
please see the part of Multiple inheritance
Tags: Inheritance in python tutorials point,Type of Inheritance in python,single Inheritance in python,single Inheritance in python, Multiple inheritance in python
If you want to learn python for free then click on python tutorials point .
BEST OF LUCK!!!
pagarsach14@gmail.com

I am Mr. Sachin pagar the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Have any Question or Comment?

Leave a Reply

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