This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I am receiving compilation error "Incompatible Types" on the line where I try to assign l.get(0) to a String. What is the return type of get() in case of generics with super keyword. l.get(0) instanceof String returns true.
Or Charsequence, Comparable and Serializable. After all, String implements these interfaces as well.
Campbell is right though; the actual type could be List<Object>, List<Serializable> (also allowing other Serializable objects like Integers) or List<Charsequence> (also allowing StringBuilder for instance). You can still put a String in the List but as the List can contain other objects you can only use the common super type to retrieve elements: Object.
Anu Kota wrote:Object is not super class of Serializable.
Object is a super type (not class) of any class or interface. That's because in the end, the root of any inheritance tree is Object.
Remember, generic bounds work with super types. That is broader than super classes; it also includes interfaces, and also array sub typing. String is a sub type of Object (i.e. you can assign a String instance to an Object reference) but String is not a sub class of Object.