Diamond operator and additional types in dynamic type

Viewed 81

I've asked myself if the diamond operator also works if not all types of the dynamic type are also given in the static type.

Look at the code: If I need the type C for a method in "Class", but I use "Interface" as static type and use the diamond operator, would that be a problem?

interface Interface<A, B> {
...
}

class Class<A, B, C> implements Interface<A, B> {
...
}

Interface<A, B> test = new Class<>();
1 Answers

This code will work just fine, you can instantiate Class without any issue. If I assume that the generic type C will be initialized through the constructor (or some other way), you may use that reference in your Interface method overridden in Class.

Related