Skip to main content

32.Python Super()

Python Super()
    The super() is used to access the methods/functions of base class.It is mostly used in multiple inheritance.It returns an object that represents the parent class.


Program
class Student:    #parent class
    def __init__(self):
        print("This is a student class")
class Marks(Student):#child class inherits Student
    pass
Sobj=Student()#parent obj
Mobj=Marks()#child obj
Output
This is a student class 
This is a student class
    Note the programs outputs two print statements.This is because the __init__() in parent class is executed twice ,one with parent object Sobj and other with child object Mobj. We know that whenever an object is created,the __init__() ,otherwise known as constructor is automatically called,so the __init__() method is called for each object created(In this case,two objects) 
Program
class Student:    #parent class
    def __init__(self):
        print("This is a student class")
class Marks(Student):#child class inherits Student
    def __init__(self):
        print("This is a Marks class")
Sobj=Student()#parent obj
Mobj=Marks()#child obj
Output
This is a student class 
This is a Marks class
    In the above program since the child class has its own __init__() ,the parent class __init__() is not executed twice.Remember,whenever an object for a child class is created,it checks whether that child class consists of __init__(),if it has __init__() on its own,only that is executed else,the __init__() in parent class is executed.
 Program
class Student:    #parent class
    def __init__(self):
        print("This is a student class")
class Marks(Student):#child class inherits Student
    def __init__(self):
        print("This is a Marks class")
Mobj=Marks()#child obj
Output
This is a Marks class
    In the above program,only the child class is created so only the __init__() of child class is executed.
Suppose,you need to access the __init__() of parent class also with the child object.That is when we use Super()
Program
class Student:    #parent class
    def __init__(self):
        print("This is a student class")
class Marks(Student):#child class inherits Student
    def __init__(self):
        super().__init__()
        print("This is a Marks class")
Mobj=Marks()#child obj
Output
This is a student class 
This is a Marks class
     In the above program,the super() is used to represent the object of parent class.super().__init__() access the init method of parent class.
Now,suppose a multiple inheritance,
Program
class Student:    #parent class
    def __init__(self):
        print("This is a student class")
class Marks:#parent class
    def __init__(self):
        print("This is a Marks class")
class Teacher(Student,Marks):#child class inherits Student , Marks
    def __init__(self):
        super().__init__()
        print("This is Teacher class")
Tobj=Teacher()#child obj
Output
This is a student class
This is Teacher class
    In the above program,the Teacher(child class) consists of two parent class.Whenever there are multiple classes,the __init__method of left class is called.[class Teacher(Student,Marks)],since Student is the left most class,the __init__() of Student class is called.This order of execution is known as Method Resolution Order.
Need to know more about python,Check out the video below!!

Happy Learning!!

Comments

Popular posts from this blog

Get Ethical Hacking Project #1

Thanks for your interest in our course This is a paid course, 150 rs for Indian people, 5$ for international people Instructions to get course: Indian people can get the course by paying using this UPI id   tamilhacks@ybl  (KARTHIKEYAN T) send 150 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)     The above mentioned method is most preferable. if you don't have a upi id app, install  phonepe  or  google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100 Rs for tax and other charges here )   After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that . the course is in zip format, so after downloading the file extra

Learn c++ in Tamil | Complete guide and tutorial | Beginner to Advance all concepts explained.

hi guys welcome , all the coding used in video are available here : /* hi guys welcome to learn c++ in tamil what is C++ how what software your own program schools,clg,institute.... start 1.interest 2.patience 3.fun 4.practise computer program - app - programming source code - hardware - fb running , executing machine language :  101010 assembly lang : mov h1; high lang : c , c++....java , js , python  eg :  a+b .....1001010  compiler :  human to machine              machine to human  rules :  a+b;  usage:  games, manufacturing , embedded , finance , graphics , etc...  Guide: 1.problem 2.solution - alternative 3.program  - C++ 4.compile  - turboc , ide , codeblocks , cmd 5.result   - library file - linker : #include<iostream.h>    input , output std::cout<<"welcome";   standard character out << " "; primary section : 1.statements   ;  end 2.functions   main() 3.syntax  , syntax error pra

Get Bug Bounty Course

Thanks for your interest in our course Limited Offer This is a paid course, Rs 10000 ,Rs 1000  for Indian people, 75$ 15$ for international people , 50$ 10$ for Srilankan people Instructions to get course: Indian people can get the course by paying using this UPI id  tamilhacks@ybl  send 1000 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)    The above mentioned method is most preferable. if you don't have a upi id app, install phonepe or google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100Rs for tax and other charges here ) After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that. As for international peopl