In Java generics you can use “&” to specify multiple interfaces as type bounds for a type parameter. This allows us for example to manipulate objects of different types with common interfaces to be manipulated uniformly even if there is not an parent interface for those common ones. My question is, how can this be used? For What purposes? I can imagine using this feature for collections, but how is it really than creating a new interface? This cannot be used dynamically, nor for type parameters. Is it just a syntactic sugar, or is there real use case for this feature?
Share
Here is an example taken from my Java Generics book:
The method above takes any source that implements both
ReadableandCloseableand any target that implements bothAppendableandCloseableand copies from source to target. You might wonder why we can’t simplify it to: