In the Realms at least, it doesnt necessarily need to be codified in law. If the king was popular or otherwise well liked, the current king might abdicate to allow them to rule until they die of old age or get sick of it. If they were incompetent or unpopular, there wont be enough support for a civil war or anything, so they just send them off to a retirement home or wherever. The desire for the throne of the heir also has a strong impact on whether the previous king gets resurrected if he is killed before his time.