
A submarine can submerge and avoid attack. They can also be surfaced for a time to run their diesel engines and recharge their batteries.
Type 21 or Type xxi, was a submarine which the German navy used during World War II. They were a major improvement over the earlier German submarines.
They were able submerge for longer periods and had greater endurance than their predecessors. Schnorchel, a new technology that allows for submergence, was what they used.

This breakthrough technology was a big step forward in the world submarines. It made it possible to completely change the way submarines were constructed and operated. The Schnorchel, a compact and small engine, could be installed inside an existing U boat's hull. This would allow it submerge for much longer times than previous designs. Also, the engine could reload torpedoes aboard while underwater.
It was a significant advancement in technology that allowed the Germans create larger and more powerful submarines which could withstand greater attacks. This allowed them to keep their crews alive, fighting longer and helped them win.
The Schnorchel's most significant feature was its ability to recharge their torpedoes under water. This was a huge advantage for the Germans. It made it easier for them to go out and hunt for enemy ships, as they didn't need to reload their torpedoes nearly as often.
Type XXI (a German U-boat) was built in Germany as the first. It was intended to become the German new submarine and help them win World War II. However, it was also a significant failure that may have helped the Third Reich to defeat.

A few years prior to the XXI, the Germans were working on a new design for submarines that would change the game. But they didn't want to gamble on this new design. They were not sure the new boat would work well, and they didn’t want to spend a lot on something that might fail.
They were able to make a prototype in April 1944, which gave them hope that production would start sooner. The new design was based on the same Walter gas turbines that were used in the original designs, but doubled by the new Deutz SAA SM517 supercharged 8-cylinder diesel engines. These engines combined with the streamlining and augmentation of the hull allowed for seventeen knots of submergence.
The gamble was a massive one for the Germans and Donitz had to make a difficult decision between giving up submarine warfare completely or gambling on the new design. He knew that if he didn't have this new boat, the war would end with him losing his crews. The boat could also reload torpedoes. This was another plus.