Question:
Define a class named Shape and its subclass Square. The Square class has an init function which takes a length as an argument. Both classes have an area function that can print the area of the shape where the Shape’s area is 0 by default.
Hints:
- To override a method in the superclass, we can define a method with the same name in the superclass.
Solution:
class Shape(object): def __init__(self): pass def area(self): return 0 class Square(Shape): def __init__(self, l): Shape.__init__(self) self.length = l def area(self): return self.length*self.length aSquare= Square(3) print aSquare.area()