0% found this document useful (0 votes)
17 views4 pages

04 (4.3 Inheritance) (3) K

Uploaded by

lsomesh448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

04 (4.3 Inheritance) (3) K

Uploaded by

lsomesh448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Ankush bhakare

Roll No.: 14
Assignment No.: 04(4.3)
Assignment Title: Develop programs to understand object oriented programming using python
(Inheritance).

Code:

4.3 Inheritance in Python:


4.3.1 Single Inheritance:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname

class Student(Person):
def printname(self):
print(self.firstname, self.lastname)

x = Student("Ketan ", "Barhate")


x.printname()

Output:
Ketan Barhate

4.3.2: Multiple Inheritance:


class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(d.Summation(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
Output:
30
200
0.5

4.3.3: Multilevel Inheritance:


class Animal:
def speak(self):
print("Animal Speaking")
#The child class Dog inherits the base class Animal
class Dog(Animal):
def bark(self):
print("dog barking")
#The child class Dogchild inherits another child class Dog
class DogChild(Dog):
def eat(self):
print("Eating bread...")
d = DogChild()
d.bark()
d.speak()
d.eat()
Output:
dog barking
Animal Speaking
Eating bread...

4.3.4: Hierarchical Inheritance:


# Base Class
class Calculate:
def __init__(self, a, b):
self.a = a
self.b = b

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")

class Platypus(Mammal, Bird):


pass

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

You might also like