• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

map is an ArrayList, compiler knows that Object is superclass of ArrayList, yet does nothing?

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enthuware question:

Consider the following code:



How can 'm' be declared such that the above code will compile and run without errors?




Below are the choices and the ones I picked are in bold

Map m = new TreeMap();
Map<Object, Object> m = new TreeMap<Object, Object>();
Map<Object, ?> m = new LinkedHashMap<Object, Object>();
Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>(); will work if lines //2 and //3 are commented out.
Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>(); will work if lines //1 and //3 are commented out.

Map m = new HashMap();



According to Enthuware,

Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>(); will work if lines //1 and //3 are commented out

is incorrect because [please note my commentary in red bold]

Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>();

You should read it aloud as follows: 'map' is declared to be of type Map that takes an instance of Object class as a key and an instance of 'a class that is either ArrayList or a superclass of Arraylist' as value. This means that the value can be an instance of ArrayList or its subclass (since an ArrayList object or its subclass object can be assigned to a reference of type ArrayList or its super class.) [but I thought the value cannot be a subclass of ArrayList as this is generics, not using regular arrays] . However, you cannot put Object (which is a superclass of ArrayList) in it because the compiler doesn't know the exact superclass that 'map' can take. It could be AbstractList, or Object, or any other super class of ArrayList. The compiler only knows that it is a superclass but not the exact type. So option 4 is correct but 5 is wrong.

Thus, you can do: m.add(anyObj, new ArrayList());



So if I understand correctly the code must explicitly say

ArrayList extends Object


I am really really confused!

Then, Enthuware gives another explanation which puzzles me even more:

Just the opposite of super is extends. Consider the following method:

public void m1(List<? extends Number> list)
{
list.add(new Integer(10)); //Error at compile time because the compiler
//only knows that list contains Number or its subclas objects. But it doesn't know the exact type.
//Therefore, it will not allow you to add anything to it.

Number n = l.get(1); //This will work because the compiler knows that every object in list IS-A Number.

}



So how would we explicitly specify that Integer IS-A Number. Or is this having to do with Wrappers and Autoboxing?

Please help!


 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>(); will work if lines //2 and //3 are commented out.
Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>(); will work if lines //1 and //3 are commented out.

THey both look same..

yes,it's right only object of type ArrayList can be added to it.

what's the correct answer???

i think except the above two ,all are correct



can be answer,then you can add anything


 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
deleted
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing is that when you write Element<? super TYPE> it means that the element can hold only a TYPE object instance or subclasses of TYPE object instance. But the reference could be assigned to anything starting from TYPE and ending on TYPE super classes.

Suppose we have



What we know at this point is that _l hold a reference to some subclass of List and that this list holds Integer or subclasses of Integer. And for sure we know that bot Integer and subclasses of Integer can be assigned to any super classes of Integer and Integer itself. That is why you cannot add anything that is higher then Integer in class hierarchy.

I hope that I made myself clear up to this point.

Regards,
Slowik
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic