Are toggle buttons a good alternative for checkboxes?

In which situation would it be better to use one or the other?

For example:

enter image description here

Vs:

enter image description here