You can absolutely do barbell overhead press and dumbbell shoulder press interchangeably. In the sense that you can switch between them every now and then or stick with the one you prefer. The weight likely wont be the same, but it isn't the weight that matters, it's the intensity. Dumbbells allow for more range of motion and require more stability. Both of which increase intensity. Don't be discouraged if you do less weight on them.
They technically work the same muscle: the anterior deltoid (the front part of the shoulder). In most shoulder press movements, you are working the front delt mostly.
However, I do not recommend that you press from behind the neck. In any shoulder press exercise, to avoid injury you should keep elbows in front. Some argue 45 degrees. This is to avoid internal rotation which can cause shoulder impingement.
To better target the lateral and rear delts there are different exercises that target them better. For the lateral the best way is probably lateral raises. These are best performed with arms about 35 degrees in front and you leaning over 10 degrees and with thumbs slightly above your pinkies. For the rear delt you can do horizontal rows, face pulls or rear delt raises.