0% found this document useful (0 votes)
35 views

Python Program

The document discusses different types of inheritance in Python including single, multiple, multilevel, hierarchical, and hybrid inheritance. It provides code examples to demonstrate each type. Single inheritance allows a child class to inherit from one parent class. Multiple inheritance allows a child class to inherit from multiple parent classes. Multilevel inheritance allows a grandchild class to inherit from a parent and grandparent. Hierarchical inheritance involves multiple classes inheriting from a common parent class but not inheriting from each other. Hybrid inheritance combines characteristics of multiple and hierarchical inheritance where a class inherits from multiple parent classes that are themselves related through inheritance.

Uploaded by

Vaishu Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Python Program

The document discusses different types of inheritance in Python including single, multiple, multilevel, hierarchical, and hybrid inheritance. It provides code examples to demonstrate each type. Single inheritance allows a child class to inherit from one parent class. Multiple inheritance allows a child class to inherit from multiple parent classes. Multilevel inheritance allows a grandchild class to inherit from a parent and grandparent. Hierarchical inheritance involves multiple classes inheriting from a common parent class but not inheriting from each other. Hybrid inheritance combines characteristics of multiple and hierarchical inheritance where a class inherits from multiple parent classes that are themselves related through inheritance.

Uploaded by

Vaishu Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#inheritance

class Person(object):
def __init__(self,name):
self.name=name
def getName(self):
return self.name
def isEmployee(self):
return False

class Employee(Person):
def isEmployee(self):
return True

emp = Person("Geek1")
print(emp.getName(),emp.isEmployee())
emp = Employee("Geek2")
print(emp.getName(),emp.isEmployee())

#single inheritance
class Parent:
def func1(self):
print("this function is in parent class")

class Child(Parent):
def func2(self):
print("this function is in child class")

object=Child()
object.func1()
object.func2()

#Multiple inheritance
class mother:
mothername=""

def mother(self):
print(self.mothername)

class father:
fathername=""

def father(self):
print(self.fathername)

class son(mother,father):
def parents(self):
print("father:",self.fathername)
print("mother:",self.mothername)

s1=son()
s1.fathername="RAMA"
s1.mothername="SITA"
s1.parents()

#multilevel inheritance
class grandfather:
def __init__(self,grandfathername):
self.grandfathername=grandfathername
class father(grandfather):
def __init__(self,fathername,grandfathername):
self.fathername=fathername
grandfather.__init__(self,grandfathername)

class son(father):
def __init__(self,sonname,fathername,grandfathername):
self.sonname=sonname
father.__init__(self,fathername,grandfathername)

def print_name(self):
print("grandfather name:",self.grandfathername)
print("father name",self.fathername)
print("son name",self.sonname)

s1=son("prince","rampal","lal mani")
print(s1.grandfathername)
s1.print_name()

#hierarchical inheritance
class parent:
def func1(self):
print("this function is a parent class")

class child1(parent):
def func2(self):
print("this function is in child1")

class child2(parent):
def func3(self):
print("this function is in child2")

object1=child1()
object2=child2()
object1.func1()
object1.func2()
object2.func1()
object2.func3()

#hybrid inheritance
class school:
def func1(self):
print("this function is in school")

class student1(school):
def func2(self):
print("this function is in student1")

class student2(school):
def func3(self):
print("this function is in student2")

class student3(school):
def func4(self):
print("this function is in student3")

object=student3()
object.func1()
object.func4()

You might also like