──────────────────────────────────────

30. Festival Room Booking · Share a Room Rule and Override It

Give a specialised room its own capacity rule and call the same method for both room types.

Lesson files · lessons/composition/override-a-method

Before you start
- Read a room’s capacity from its attributes.
- Use a method to answer whether a group fits.

By the end
- Override the workshop seat rule while keeping its usable seats at zero or above.
- Use the two room outputs to explain which usable_seats() method each object calls.

Where you can use it
Use the same booking request for an ordinary room and a workshop room that holds back seats for facilitators.

class WorkshopRoom(Room) defines a subclass of Room. It inherits Room’s constructor and methods. Writing a method with the same name in the subclass overrides the inherited method. Both room types can use fits(). When fits() calls self.usable_seats(), Python uses the method for the object in self. This is polymorphism, where the same call can produce behaviour specific to the kind of object receiving it.

Predict
Which version of usable_seats runs when fits checks the Nest?
- The one in Room.
- The one in QuietRoom.

Worked example (example.py)

class Room:
    def __init__(self, name, capacity):
        self.name = name
        self.capacity = capacity

    def usable_seats(self):
        return self.capacity

    def fits(self, people):
        return people <= self.usable_seats()


class QuietRoom(Room):
    def usable_seats(self):
        return self.capacity // 2


rooms = [Room("Studio", 8), QuietRoom("Nest", 8)]
for room in rooms:
    print(f"{room.name}: {room.fits(5)}")


Example result
Studio: True
Nest: False

Override usable_seats in WorkshopRoom to reserve two seats for facilitators, down to a minimum of zero usable seats. Leave fits in Room. Print whether the supplied group fits each of the two rooms.

Starter program (starter.py)

class Room:
    def __init__(self, name, capacity):
        self.name = name
        self.capacity = capacity

    def usable_seats(self):
        return self.capacity

    def fits(self, people):
        return people <= self.usable_seats()


class WorkshopRoom(Room):
    pass


people = int(input())
rooms = [Room("Studio", 8), WorkshopRoom("Lab", 8)]
for room in rooms:
    print(f"{room.name}: {room.fits(people)}")


Starter input
7

Hint
Use max(0, self.capacity - 2) in the override. max picks the larger of the two values.

Case 1. Try the sample
Input
7
Expected output
Studio: True
Lab: False

Case 2. Both fit at the workshop limit
Input
6
Expected output
Studio: True
Lab: True

Case 3. Both rooms are too small
Input
9
Expected output
Studio: False
Lab: False

Stretch
Create a WorkshopRoom with one seat. Print its usable seat count to check the floor of zero.

Reflect
What part of the booking rule does WorkshopRoom share with Room?

Check your prediction
The one in QuietRoom. The inherited fits method calls self.usable_seats() and self is a QuietRoom instance.
