Be careful not to confuse monitors with the objects they belong to. Generally, it is best to entirely forget about the "object" aspect when thinking about monitors and locks.
Especially, the class where a certain piece of code is declared has nothing to do with the locks being acquired. When you lock Demo.kitchen
inside a Bathroom
's method, only the kitchen
object is locked. Also, locking Demo.kitchen
will not prevent other code to concurrently access and change the kitchen
object—unless that code is also protected by the same lock.