Sonar is a way of detecting objects underwater by emitting sound pulses and then detecting and measuring their return after being reflected back. Dolphins use sonar and so do submarines. These are two very different things. One is an aquatic mammal and the other is a mere machine but they use the very same method for detecting things underwater.
Another type of animal that doesn’t live in the water but uses sonar as well is the bat. Bats use sonar to help them hunt for food at night. They do not have good sight but with sonar they do not need it. A dolphin’s sonar is so precise that it can even detect the difference between a golf ball and a ping pong ball based solely on density.