- Walls have been militarily obsolete since the invention of gunpowder
This is just not true enough in just the right kind of way that it deserves mention. While obviously a castle with vertical walls loses a lot of effectiveness against cannon balls, Europe was not about to give up so easily. Fortification and European society went hand-in-hand so well that Europe simply started building different castles.
Without getting really boring about this because it's already an obnoxious digression, star forts were such a bitch to attack and design that artisans from foreign countries were brought in and professional mercenaries were hired to attack them, effectively giving rise to a middle class that weren't serfs and weren't royals. As the cost of laying siege to an effective star fort became ruinous, diplomacy and alliances replaced the feudal warfare of the Middle ages. And as diplomacy and alliances replaced feudal warfare, the star forts mostly went away.
Sorry. Carry on. I just find these sorts of things fascinating.