Cavities are caused by bacteria that is in your mouth which decays your teeth and causes the cavities in your teeth.
The bacteria can be spread from person to person through kissing and also through sneezing and sharing food and utensils such as fork and spoon.
The bacteria that causes cavities is contagious and you can get rid of the bacteria that causes cavities by using Apple Cider Vinegar as a mouth wash since the Apple Cider Vinegar is antibacterial which kills the bacteria that causes cavities.
You can also use Coconut Oil to do oil pulling which helps kill bacteria in your mouth and prevent cavities.
If the tooth cavity isn't too bad then you can use demic toothpaste that sticks to the tooth and fills in small cavities but if the cavity is too large you need to have the cavity filled in by a dentist.