Given the method definiton
public void foo(List<Object>) { ... }
What happens if you want to call
foo(new ArrayList<String>);
Based on your question above I deduce your foo method to be as follows
public void foo(List<Object> ob)
{
//Lines of code;
}
Now based on the method call you have shown there can be two cases I can deduce
1. foo(new ArrayList<String>);--- Without bracket after String
&
2.foo(new ArrayList<String>());-- With Bracket after String
I will tell you what will happen in both the cases, for the first case it will give the below error
error: '(' or '[' expected as you are missing the parenthesis after the constructor.
For the second case if there is the() bracket . We will still get an error
error: incompatible types: ArrayList cannot be converted to List
even though String is a sub-class of Object as in Generic type polymorphism is not allowed it is only applicable to collection types.
if it were foo(new ArrayList<Object>()); or foo(new ArrayList<? extends Object>()); both would have been valid and the program would have executed successfully.
Get Answers For Free
Most questions answered within 1 hours.