Skip to main content

30.Python Inheritance

Python Inheritance
    Object oriented programming has its own features like inheritance,polymorphism etc.Inheritance is one of the fundamental parts of Object oriented programming.It allows to extend the classes by inheriting the properties and methods of parent class.In short,Inheritance is the special feature which is used to inherit the properties of parent class to the child class.The parent class is also called as the base class.The child class is also called as derived class.
For example,
Consider two classes School and Student.The School class consists information about name of the school  and place where the school is located.The student class contains information about name of the student and age of the student.Now say,the student class must also know the information of the school class.For that case,we can use the inheritance.

Syntax:
class ParentclassName:
    methods/attributes
class className(ParentclassName):
    methods/attributes
Program:
class School:#parent class
    def schoolInfo(self):#parent class method
            print("Welcome to school")
class Student(School):#child class
    def studentInfo(self):#child class method
        print("I am student")
schoolobj=School()#parent object
stu=Student()#student object
stu.schoolInfo()#accessing parent method with child object
stu.studentInfo()#accessing child method with child object.
Output:
Welcome to school 
I am student
    As said earlier,Not only the Parent class methods can be inherited,but also the variables/attributes.
Program:
class School:#parent class
    def __init__(self,name,place):#variable initialization
        self.name=name
        self.place=place
    def schoolInfo(self):#parent method
            print(f"Welcome to school\nName of the school:self.name}\nPlace of the school:{self.place}")
 class Student(School):#child class
    def __init__(self,sname,age,name,place):
        self.sname=sname
        self.age=age
        self.name=name
        self.place=place
    def studentInfo(self):#child method
        print(f"I am student\nName of the Student:{self.sname}\nAge:{self.age}")
        print(f"My school name is:{self.name}\nIt is located        in{self.place}")
        
sobj=School("TamilHacks","India")#parent obj
stu=Student("Harry",18,sobj.name,sobj.place)#child obj
stu.studentInfo()#accessing the method
Output:
I am student 
Name of the Student:Harry 
Age:18 
My school name is:TamilHacks 
It is located in India
Note that,
  • In the child object when needed to use the variables declared in the parent class,it must mentioned as parentobj.variablename.(sobj.name in the above program)
            sobj=School("TamilHacks","India")#parent obj
            stu=Student("Harry",18,sobj.name,sobj.place)#child obj
    Need to know more about python,check out the link 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