KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
public void wahey(List<Object> list) {} wahey(new LinkedList<Number>()); The call to the method will not type-check. I can't even cast the parameter as follows: wahey((List<Object>) new LinkedList<Number>()); From my research, I have gathered that the reason for not allowing this is type-safety. If we were allowed to do the above, then we could have the following: List<Double> ld; wahey(ld); Inside the method wahey, we could add some Strings to the input list (as the parameter maintains a List<Object> reference). Now, after the method call, ld refers to a list with a type List<Double> , but the actual list contains some String objects! This seems different to the normal way Java works without generics. For instance: Object o; Double d; String s; o = s; d = (Double) o; What we are doing here is essentially the same thing, except this will pass compile-time checks and only fail at run-time. The version with Lists won't compile. This leads me to believe this is purely a design decision with regards to the type restrictions on generics. I was hoping to get some comments on this decision?
Tags (comma-separated)
Save Edits
Cancel