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

Practical14.ipynb - Colaboratory

The document contains three Python code examples demonstrating class inheritance and polymorphism. The first example shows two classes with the same method name printing different outputs. The second calculates the area of a square or rectangle. The third shows inheritance where subclasses override a parent class method to provide specific output.

Uploaded by

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

Practical14.ipynb - Colaboratory

The document contains three Python code examples demonstrating class inheritance and polymorphism. The first example shows two classes with the same method name printing different outputs. The second calculates the area of a square or rectangle. The third shows inheritance where subclasses override a parent class method to provide specific output.

Uploaded by

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

#Q1)

class a:
def same(self,a,b):
self.a=a
self.b=b
print("a:",a,"b:",b)
class b:
def same(self,a,b):
self.a=a
self.b=b
print("a:",a,"b:",b)

obj1=a()
obj1.same("apple",23)
obj2=b()
obj2.same(45,"mango")

a: apple b: 23
a: 45 b: mango

#Q2)
class AreaCalculator:
def area(self, length, breadth=None):
if breadth is None:
return length * length
else:
return length * breadth

calculator = AreaCalculator()

square_area = calculator.area(5)
print("Area of square:", square_area)

rectangle_area = calculator.area(4, 6)
print("Area of rectangle:", rectangle_area)

Area of square: 25
Area of rectangle: 24

#Q3)
class Degree:
def getDegree(self):
print("I got a degree")
class undergraduate(Degree):
def getDegree(self):
print("I an undergraduate")
class postgraduate(Degree):
def getDegree(self):
print("I an postgraduate")
a=Degree()
a.getDegree()

b=undergraduate()
b.getDegree()

c=postgraduate()
c.getDegree()

output I got a degree


I an undergraduate
I an postgraduate

add Code add Text

You might also like