In object oriented programming, should inanimate objects have methods/behaviour?

Viewed 64

I am a new user to programming and have started learning object oriented programming. It seems OOP is used to model real-life objects and interactions. My question is, should inanimate objects have behaviour?

For example should I have something like

class Room:
    clean()

or

class User:
    clean_room(Room)
1 Answers

Good question! You should have both.

The room can definitely have a clean() method, even if it's inanimate. In fact, this is practically necessary. If you pass a room to the User they will still have to call some public method in the Room class to perform the cleaning. clean() would be a suitable method for this. In fact, public methods exist so they can be used from the outside of the class, by some other class.

To be able to tell the User that he/she should clean the room, you also need a clean_room() method in the User class. This method will then call the clean() method on the Room object.

Related