04 (4.3 Inheritance) (3) K
04 (4.3 Inheritance) (3) K
Roll No.: 14
Assignment No.: 04(4.3)
Assignment Title: Develop programs to understand object oriented programming using python
(Inheritance).
Code:
class Student(Person):
def printname(self):
print(self.firstname, self.lastname)
Output:
Ketan Barhate
def division(self):
print(self.a / self.b)
#Derived Class
class Add(Calculate):
def __init__(self, a, b):
Calculate.__init__(self, a, b)
def add(self):
print("Addition:", self.a + self.b)
Add.division(self)
#Derived Class
class Subtract(Calculate):
def __init__(self, a, b):
Calculate.__init__(self, a, b)
def subtract(self):
print("Subtraction:", self.a - self.b)
Subtract.division(self)
obj1 = Add(4, 2)
obj2 = Subtract(5, 4)
obj1.add()
obj2.subtract()
Output:
Addition: 6
2.0
Subtraction: 1
1.25
4.3.5: Hybrid Inheritance:
class Animal:
def speak(self):
print("Animal speaks")
class Mammal(Animal):
def give_birth(self):
print("Mammal gives birth")
class Bird(Animal):
def lay_eggs(self):
print("Bird lays eggs")
platypus = Platypus()
platypus.speak() # Method from Animal class
platypus.give_birth() # Method from Mammal class
platypus.lay_eggs() # Method from Bird class
Output:
Animal speaks
Mammal gives birth
Bird lays eggs