I read a few pages on why they stopped using them that's easy to find but can't find anything on what the next and superior defense was and I was curious.
Advances in artillery made castles obsolete during the late fourteenth and fifteenth centuries, mainly because their walls were tall but relatively thin ("curtain walls"). This was because they were intended to stop assaulting troops, not powerful artillery. The fortifications that replaced them starting in the fifteenth century were constructed with angled walls that were thick to resist artillery, while also having outward bastions to mount defensive guns and troops. These were called trace italienne because they became common in Italy during the late 14th century wars between the small Italian states. If you look up trace italienne in an image search you can find examples. The angles reduced blind spots and gave the defenders kill zones to push back attackers, and this put the offensive army at a serious disadvantage.
They really "came of age" during the Italian Wars from 1494-1559 and the Ottoman campaigns in southeastern Europe from 1521-1566. They also dramatically reshaped military tactics in some theaters of warfare, making siege the dominant objective instead of battle, although in places like France during their wars of religion this was less true. Overall, the power of these fortifications is why you will see relatively few large-scale battles in the sixteenth century. For example, Suleiman the Magnificent's major field battle in Europe was at Mohác in 1526. The rest of his campaigns consisted mostly of sieges and the next big battle wasn't until Mehmed III accompanied his army at Keresztes in 1596. The Thirty Years War also many traditional field battles despite the power of fortifications during the 17th century. The overall dominance of forts in warfare started to break around the War of Spanish Succession with generals like the Duke of Marlborough in England and others focused on traditional maneuvers, leading to the transition to eighteenth century style warfare in conflicts like the Seven Years' War or American War of Independence.