The vast majority of single-occupancy public toilets (not cubicles with a shared space outside for handwashing and drying, but a single room containing both toilet and sink) will have an indication for occupancy. I have seen this in restaurants, on trains, and in aeroplanes.
Very few such toilets (none, in my experience) will have closing and locking the door be a single action. Otherwise, it would be easy for someone to leave an empty toilet locked. Usually, there is a physical latch which can be operated only from the inside. Sliding the latch will both lock the door and move a small panel on the outside. On Irish trains, there is a little toilet symbol at one end of the carriage, so passengers know in which direction to head: this will also turn off when the toilet is occupied. Wheelchair-accessible toilets on trains have a lock button rather than a physical latch: this turns off the green light on the “door open” button on the outside.
On aeroplanes when the passenger closes the toilet door it automatically locks the door and changes the status to occupied in the same action as closing the door.
It’s a while since I’ve been on a plane, but I think that the way the door is shoved from the inside might activate an automatic locking system. This is unusual and no doubt fiddly to build (and you certainly wouldn’t want to activate it from the outside accidentally), but might be necessary on planes: if people could be in the toilet without locking the door and activating the “occupancy” notification outside, they could hide there, which might be a bad thing.
That degree of complexity is unnecessary elsewhere, so it isn’t used.