5. Rectangle Inheritance

5.1. Square(Rectangle) Inheritance

In the code below, the Square class uses the super() function to modify the __init__ method that would be inherited from the Rectangle class.
Write code to ouput the area of a square of side length 3.

Tasks

  1. Write a Square(Rectangle) class using the scaffold below.

    class Rectangle:
    
        def __init__(self, length, width):
            self.length = length
            self.width = width
    
        def area(self):
            return self.length * self.width
    
    
    class Square(           ):
    
        def __init__(self, length):
            super().__init__(       ,       )
    
    square = Square(3)
    print(square.area())
    

Write a Square(Rectangle) class.

class Rectangle:

    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width


class Square(Rectangle):

    def __init__(self, length):
        super().__init__(length, length)

square = Square(3)
print(square.area())

5.2. ColouredRectangle(Rectangle) Inheritance

In the code below, the ColouredRectangle class has its own __init__ method that uses the super() function to reuse the __init__ method from the Rectangle class and to allow other attributes to be set separately.
Write could to output:
Area of the red rectangle is 6.

Tasks

  1. Write a ColouredRectangle(Rectangle) class using the scaffold below.

    class Rectangle:
        def __init__(self, length, width):
            self.length = length
            self.width = width
    
        def area(self):
            return self.length * self.width
    
    class ColouredRectangle(          ):
        def __init__(self, length, width, colour):
            super().__init__(       ,      )
            self.colour =
    
    col_rect = ColouredRectangle(2, 3, 'red')
    print(f'Area of the {col_rect.colour} rectangle is {col_rect.area()}.')
    

Write a ColouredRectangle(Rectangle) class.

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

class ColouredRectangle(Rectangle):
    def __init__(self, length, width, colour):
        super().__init__(length, width)
        self.colour = colour

col_rect = ColouredRectangle(2, 3, 'red')
print(f'Area of the {col_rect.colour} rectangle is {col_rect.area()}.')