All perfect praise be to Allaah, The Lord of the worlds. I testify that there is none worthy of worship except Allaah and that Muhammad is His slave and Messenger.
The worldly sciences that people need for their benefits and livelihood, such as medicine, engineering, physics, chemistry and so on, are among the communal sciences that some Muslims should specialize in, and learning them may become obligatory upon someone who is more competent in learning them than others, because if they were totally abandoned, then societies would be weakened and some acts of worship would be hindered; because of that, Muslims would be in need for non-Muslims.
Ibn Al-Qayyim said in At-Turuq Al-Hukmiyyah, “For example, people would need a range of professions, such as agriculture, textile industry (weaving), and construction, and so forth, so the ruler has the right to force them to do so by paying them a fair fee, as the public interest would not be achieved otherwise. It is for this reason that a group of scholars from the Hanbali and Shaafi’i schools stated that learning such industries is a communal obligation because of the need of the people for it.”
It is known that there is a reward in doing communal obligations; the one who does it is a representative of the nation in making them exempt from obligation and sin.
If some people learn these sciences in order to exempt the nation from the communal obligation, then learning them remains permissible in principle. With a good intention, what is permissible may become an act of worship by which one gets closer to Allaah and an obedience for which he will be rewarded. Abu Tharr narrated that the Prophet said, “And in a man's sexual intercourse (with his wife), there is Sadaqah (charity).” They (the Companions) said, “O Messenger of Allaah, is there a reward for the one who satisfies his sexual desire?” He said, “Tell me, if he were to fulfill his desire in a forbidden manner (illegitimately), would it not be a sin on his part? Similarly, if he were to fulfill his desire in a lawful manner (with his wife), he will get a reward.” [Muslim]
An-Nawawi said while explaining the benefits of this hadeeth, “This hadeeth is evidence that if one intends to obey Allaah by doing what is permissible, it will become an act of obedience and he will be rewarded for it. The Prophet highlighted this by saying, 'Even if it were a morsel of food that you put in the mouth of your wife [you will be rewarded for it]...'”
Allaah knows best.