This is quite an interesting question. I didn’t know the answer to this so did a quick reading online.
Apparently all birds are descendant of ancestors that could fly. Some birds like ostrich, penguins evolved in a way which made them lose their ability to fly. This could be explained by the environment these birds lived in. For e.g. penguins lived in Antarctica and fed on fish where flying was not necessary but swimming was. This made their body short and stubby and didn’t require them to fly. Same applies to other flightless birds like chicken or ostrich.
Comments