Inheritance 

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multi-level-Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

#Python program that demonstrates Single Inheritance
class person:
def __init__(self, name, age):
self.name=name
self.age=age
def display(self):
print(f”I am {self.name} and I am {self.age}years old”)
class Employee(person):
def __init__(self, name, age, salary):
super().__init__(name, age)
self.salary=salary
def display(self):
super().display()
print(f”My salary is {self.salary}”)
e=Employee(“Suresh”, 33, 55000)
e.display()

OUTPUT:

I am Suresh and I am 33years old
My salary is 55000

2.

#Python program that demonstrates Multiple Inheritance
class Person:
def __init__(self,name,age):
self.name=name
self.age=age
def display(self):
print(f”I am {self.name} and I am {self.age}year old”)
class Student:
def __init__(self, roll_no, marks):
self.roll_no = roll_no
self.marks=marks
def display(self):
print(f”My roll number is {self.roll_no}and my marks are {self.marks}”)
class Employee(Person, Student):
def __init__(self, name, age, roll_no, marks, salary):
Person.__init__(self,name, age)
Student.__init__(self, roll_no, marks)
self.salary=salary
def display(self):
super().display()
Student.display(self)
print(f”My salary is {self.salary}”)
e=Employee(“Venkat”,29, 101, 90, 55000)
e.display()

OUTPUT:

I am Venkat and I am 29year old
My roll number is 101and my marks are 90
My salary is 55000

3.

#Python Program that demonstrates Multilevel Inheritance
#Define base class as ‘Student’
class Student:
def getStudent(self):
self.name=input(“Enter Name:”)
self.age=input(“Enter Age:”)
self.gender=input(“Enter Gender:”)
# Define a class as ‘Test’ and inherit base class ‘Student’
class Test(Student):
def getMarks(self):
self.stuYearSem=input(“Enter your Year and Semester:”)
print(“Enter the marks of the respective subjects”)
self.sub1=int(input(“IME:”))
self.sub2=int(input(“Java:”))
self.sub3=int(input(“Python:”))
self.sub4=int(input(“Android Programming:”))
self.sub5=int(input(“CNS:”))
#Define a class as ‘Marks’ and Inherit derived class ‘Test’
class Marks(Test):
#Method
def display(self):
print(“\n\nName:”,self.name)
print(“Age:”,self.age)
print(“Gender:”,self.gender)
print(“Study in:”,self.stuYearSem)
print(“Total Marks:”, self.sub1+self.sub2+self.sub3+self.sub4+self.sub5)
print(“Average Marks:”,(self.sub1+self.sub2+self.sub3+self.sub4+self.sub5)/5)
m1=Marks()
#Callbase class method ‘getStudent()’
m1.getStudent()
#Call first derived class method ‘getMarks()’
m1.getMarks()
#Call second derived class method ‘display()’
m1.display()

OUTPUT:

Enter Name:aky
Enter Age:30
Enter Gender:female
Enter your Year and Semester:iii sem 1st year
Enter the marks of the respective subjects
IME:450
Java:90
Python:95
Android Programming:96
CNS:98

Name: aky
Age: 30
Gender: female
Study in: iii sem 1st year
Total Marks: 829
Average Marks: 165.8

4.

#Python Program that demonstrates Hierarchical Inheritance
class First:
def __init__(self):
self.x=20
self.y=10
class Second(First):
def findsum(self):
self.z = self.x + self.y
print(“Sum is:”, self.z)
class Third(First):
def findsub(self):
self.z = self.x – self.y
print(“Subtraction is:”,self.z)
obj1=Second()
obj1.findsum()

obj2=Third()
obj2.findsub()

 

OUTPUT:

Sum is: 30
Subtraction is: 10

5.

#Python program that demonstrates Hybrid Inheritance
class Animal:
def __init__(self, name):
self.name=name
def eat(self):
print(f”{self.name} is eating”)
class Mammal(Animal):
def __init__(self, name):
super().__init__(name)
def walk(self):
print(f”{self.name} is walking”)
class Bird(Animal):
def __init__(self,name):
super().__init__(name)
def fly(self):
print(f”{self.name} is flying”)
class Bat(Mammal, Bird):
def __init__(self,name):
super().__init__(name)
b=Bat(“Batty”)
b.eat()
b.walk()
b.fly()

OUTPUT:

Batty is eating
Batty is walking
Batty is flying

6.

#Another Example that demonstrates Hybrid Inheritance
class A:
def method_A(self):
print(“This is from class A.”)
class B(A):
def method_B(self):
print(“This is from class B.”)
class C(A):
def method_C(self):
print(“This is from class C.”)
class D(B,C):
def method_D(self):
print(“This is from class D.”)
# create an object of class D
d=D()
# call methods from class A, B, C, and D
d.method_A()
d.method_B()
d.method_C()
d.method_D()

OUTPUT:

This is from class A.
This is from class B.
This is from class C.
This is from class D.

 

 

FAQ:

Leave a Reply

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