Kin selection
Kin selection is an evolutionary strategy where some specific behavioral traits are favored to benefit close relatives' reproduction. Hence, behavior that appears altruistic can align with the theory of the selfish gene. Kin selection can explain altruistic behavior towards close relatives even at the cost of their own's survival, as long as one's sacrifice can help preserve a greater amount of the same genes in close relatives. For example, worker bees can die from attacking their predators in order to help preserve other bees' genes.