Inheritance is concept where one class accesses the methods and properties of another class.
There are two types of inheritance in python −
In multiple inheritance one child class can inherit multiple parent classes.
class Father: fathername = "" def father(self): print(self.fathername) class Mother: mothername = "" def mother(self): print(self.mothername) class Daughter(Father, Mother): def parent(self): print("Father :", self.fathername) print("Mother :", self.mothername) s1 = Daughter() s1.fathername = "Srinivas" s1.mothername = "Anjali" s1.parent()
Father : Srinivas Mother : Anjali
In this type of inheritance, a class can inherit from a child class/derived class.
#Daughter class inherited from Father and Mother classes which derived from Family class. class Family: def family(self): print("This is My family:") class Father(Family): fathername = "" def father(self): print(self.fathername) class Mother(Family): mothername = "" def mother(self): print(self.mothername) class Daughter(Father, Mother): def parent(self): print("Father :", self.fathername) print("Mother :", self.mothername) s1 = Daughter() s1.fathername = "Srinivas" s1.mothername = "Anjali" s1.family() s1.parent()
This is My family: Father : Srinivas Mother : Anjali