Yes, Germany had a need to defend its coast. But building up the HSF to threaten the RN was a strategic mistake that soured UK-German relations, just for stroking the ego of the idiot Kaiser.
Yes, challenging the RN to build race was stupid.
Matching or slightly exceeding the French might have been less provocative, and a lot cheaper.
Germans might have managed with 12-16 heavy ships instead of 25 or so.
They might have gotten away with even less as the 6 Dantons were neither fish nor fowl. Four 12 in guns and twelve 9.4in (6 per side) might work in a short-range battle, not very good if the range was long, like over 13,000 meters.
The question of coastal fortifications vs navies is interesting. I suspect sometime in the first half of the 20th century there was a kind of inflection point as technology developed to the point of making expensive fixed fortifications mostly obsolete.
Sea going ships vs coastal fortifications is interesting and they had several hundred years to work it out until the Crimean war.
After that it was race much like many other weapons races.
Steam gave better speeds (marginally) and freedom from wind/weather/currents.
Improved metallurgy gave larger guns that could fire 8in and large exploding shells (still balls) that could hurt forts much more than solid round shot.
Ships also started to be covered in iron (at least a few) that could resist smaller shore-based guns. Large ones were still a problem.
By the 1860s they had rifling which allowed for elongated projectiles for better penetration and range, better steam engines, a lot more armor (although sometimes trying use 6-8in worth of 1 in plates didn't work so well).
And then we start getting into basic heat treatment. Cocoa (Brown) powder (1870s-90s) even bigger guns, really slow firing. Steel armor instead of iron.
Fort geography changed. Longer ranged guns meant you could move the gun batteries further from the choke points. Fort architecture changed with hidden guns behind sloped walls instead of gun ports in vertical walls. Smokeless powder made for a huge increase in range and shore guns began to get better fire control. And then the fire control race started
Who was ahead at any given moment is hard to tell, unfortunately land based forts tended to stay in use for longer than most iron or steel ships so many forts could be out matched by more modern ships.