Have you ever needed to enforce that a List implements RandomAccess? There is a better solution than using “instanceof.” Let’s take a look at how generics can make multiple inheritance more accessible in Java.
Given two interfaces, how do you enforce that a method parameter extends both?
One way is to create a third interface that extends both interfaces and use that.
The problem with this solution is that you now have this interface that serves no real purpose.
As an alternative, we can use generics to enforce this at compile time and get rid of the additional interface entirely.
This comes in handy when we want to enforce that a List object is optimized for indexed access. We know that accessing the entries by index efficiently.
This shows how we can safely implement multiple inheritance. The Java compiler enforces that the entries after the ampersand are all interfaces.