Here are several questions related to type mismatch using generics in Java, but I was not able to find anything corresponding to my case.
I wonder why this error happens in below code?
Type mismatch: cannot convert from element type Object to String
in line
for (String element : arg.someMethod())
But if
SomeInterface arg
is changed to
SomeInterface<?> arg
it works. Why list parameter type is erased if it is not connected to interface type parameter?
import java.util.List;
public class TypeMismatchExample
{
interface SomeInterface<P extends Object>
{
P someParametrizedMethod();
List<String> someMethod();
}
void example(SomeInterface arg)
{
for (String element : arg.someMethod())
{
// do something
}
}
}