If we're talking the kind of monarchy that we have here in the UK, where the rules about the line of succession are fixed, then an heir's ability to have children is irrelevant -- we've had childless monarchs on a number of occasions eg William Rufus, Mary, Elizabeth, Edward VI, Charles II, WilliamandMary, Anne, William IV (though, of course, we know now they never had children, but that wasn't foreseen when they came to the throne). In this case, the heir inherits and remains on the throne until death or abdication, whereupon the next in line takes over ie sibling, niece/nephew, cousin, whatever.
At a time when it's important to have a strong monarch, so it's also important to have heirs of the body (ie sons, usually) and especially n the absence of codified rules about succession, then there might well be a bit of jostling for the throne, and the strongest person will win (eg all the Wars of the Roses stuff). Or if those being governed have a say in it and they don't like the look of the monarch's possible heirs, they might invite someone else to take the throne (eg James II being deposed as his son would be Catholic and W&M being brought over from the Ntherlands).
There's nothing to stop a king abdicating if he feels his heir would do a better job -- present day Netherlands springs to mind in this context.
NB In all the systems I know of, it's the elder/eldest son who will inherit before the younger one(s), though an older daughter may well be subordinate to a younger brother (so with us, Princess Anne ranks lower in the succession than Princes Andrew and Edward).
NB2 If an heir is too young to take the throne, usually a regent would be appointed to look after things during the heir's minority eg Edward VI. ie they wouldn't crown another monarch pro tem.