There is a design principle stating that you should favor composition over inheritance but isn’t it true that this doesn’t hold when we want to make use of polymorphism?
For example if we want to do something for several related objects, we might want to iterate over a for-each-loop only stating that the object performing the method is-a some supertype, how would we get that functionality and code reuse using composition?
In statically typed languages where polymorphism is tied to inheritance, that inheritance can be achieved in two ways: one way is stateful and the other way is stateless. These may be referred to as implementation inheritance versus specification inheritance, respectively. For example, in Java, class inheritance is stateful whereas interface inheritance is stateless.
With that in mind, when the Gang of Four book says,
Favor object composition over class inheritance.
...they are referring to stateful inheritance. So polymorphism can still be achieved through stateless inheritance while following this principle, even in statically typed languages.
(In dynamically typed languages there is no relationship between polymorphism and inheritance, because polymorphism does not require explicit type declarations.)
What the Gang of Four were worried about was this.
...designers often overuse inheritance as a reuse technique, [but] designs are often made more reusable (and simpler) by depending more on object composition.
I often phrase this as, "inheritance is a poor tool for code reuse." In other words, don't use inheritance to keep your code DRY. Composition is better for that. Stateless inheritance, on the other hand, is not intended for code reuse and is a great tool for polymorphism.