Oracle certified JPA Developer (1Z0-898),Oracle certified Java 8 Programmer I (1Z0-808), Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
Programmer Analyst || J2EE web development/design
Oracle certified JPA Developer (1Z0-898),Oracle certified Java 8 Programmer I (1Z0-808), Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
SCJA
~Currently preparing for SCJP6
Programmer Analyst || J2EE web development/design
reji singh wrote:
The below code does not compile
NavigableSet<? extends Object> set = new TreeSet<Object>();
set.add(new Object());
All code in my posts, unless a source is explicitly mentioned, is my own.
Ruben Soto wrote:
reji singh wrote:
The below code does not compile
NavigableSet<? extends Object> set = new TreeSet<Object>();
set.add(new Object());
When you are doing set.add(new Object()) the only thing the compiler knows is that set is a NavigableSet<? extends Object> reference, and the addition operation will break the collection type guarantee sometimes. To see that, try this:
NavigableSet<? extends Object> set = new TreeSet<String>(); // Valid declaration and initialization
set.add(new Object()); // This is a problem, adding an Object to a collection of String
However, when you use a NavigableSet<? super X> reference, you can add X elements to it with no problem, because you are guaranteed that the actual collection object is of type X or a supertype of X, so adding a X element is OK.
reji singh wrote:
Thanks Ruben, i still feel that I am not clear. Ok, consider this.
NavigableSet<? extends Object> set = new TreeSet<String>();//declaration same as per your comment
set.add("string");// why this is not ok as we are adding string to collection os strings , isnt it?
NavigableSet<? super Object> set2 = new TreeSet<Object>();
set2.add( "string");// NavigableSet<? super Object> --- here the actual collection object is String which is not supertype of X- Object. So, why is it allowed?
All code in my posts, unless a source is explicitly mentioned, is my own.
reji singh wrote:Hi Ruben,
Many thanks. Its very clear now. I was not able to connect all these things together to have a bigger picture. Hopefully, I will score better now in mocks and will apply the concepts in a better way at work.
Thanks you!
All code in my posts, unless a source is explicitly mentioned, is my own.
Programmer Analyst || J2EE web development/design
Oracle certified JPA Developer (1Z0-898),Oracle certified Java 8 Programmer I (1Z0-808), Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
Oracle certified JPA Developer (1Z0-898),Oracle certified Java 8 Programmer I (1Z0-808), Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
Don't get me started about those stupid light bulbs. |