Java Interview Question

Use this intermediate question and answer to prepare for your next Java interview.

Why won't the following code sample compile? How can you correct it?

class A {
  public static void main(String...args) {
    List<String> ls = new ArrayList<String>();

  public void t1(List<Object> lo) { }

This will raise a compilation error. It is incorrect to pass a List<String> to a method accepting List<Object>. A List<Object> can accept Strings, but t1 can only accept List<Object>.

Either ls should be changed to List<Object>, or lo should be made Generic: List<? extends Object> lo

Looking for developers?