•Create a class named Shape with a function that prints "This is a shape". Create another class named Polygon inheriting the Shape class with the same function that prints "Polygon is a shape". Create two other classes named Rectangle and Triangle having the same function and inherited from polygon class, which prints "Rectangle is a polygon" and "Triangle is a polygon" respectively. Again, make another class named Square inherited from rectangle having the same function which prints "Square is a rectangle".
Now, try calling the function by the object of each of these classes.
class Shape():
def display(self):
print("This is a shape")
class Polygon(Shape):
def display(self):
print("Polygon is a shape")
class Rectangle(Polygon):
def display(self):
print("Rectangle is a polygon")
class Triangle(Polygon):
def display(self):
print("Triangle is a polygon")
class Square(Polygon):
def display(self):
print("Square is a rectangle")
shape=Shape()
polygon=Polygon()
rectangle=Rectangle()
triangle=Triangle()
square=Square()
shape.display()
polygon.display()
rectangle.display()
triangle.display()
square.display()
Comments
Leave a comment