InterviewSolution
| 1. |
Can You Pass List<string> To A Method Which Accepts List<object>? |
|
Answer» This generic interview question in Java may look CONFUSING to any ONE who is not very familiar with Generics as in fist glance it LOOKS LIKE String is OBJECT so List<String> can be used where List<Object> is required but this is not true. It will result in compilation error. It does make sense if you go one step further because List<Object> can store any any thing including String, Integer etc but List<String> can only store Strings. 1List<Object> objectList; 2 List<String> stringList; 3 4 objectList = stringList; //compilation error incompatible types This generic interview question in Java may look confusing to any one who is not very familiar with Generics as in fist glance it looks like String is object so List<String> can be used where List<Object> is required but this is not true. It will result in compilation error. It does make sense if you go one step further because List<Object> can store any any thing including String, Integer etc but List<String> can only store Strings. 1List<Object> objectList; 2 List<String> stringList; 3 4 objectList = stringList; //compilation error incompatible types |
|