It is not about the survival of the individual bee. The bee that stings you was never going to reproduce anyway only the drones and queens get to do that.
By sacrificing themselves they help their hive, which shares their traits to survive and helps pass on their genes indirectly.
It helps other animals to learn and evolve to stay away from bees and that is a great benefit to bees in general at the cost of a bee which would only have lived for a few more months anyway.
That is a good trade.
Latest Answers