Answer to Question #260394 in Python for fatima

Question #260394

•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.


1
Expert's answer
2021-11-03T00:47:09-0400
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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog