Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

generic type instantiation question

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
generic is my weak point and it is the area that I will lose points in OCP exam. I have problem understanding K&B Practice Exam 4 (34). Would someone please explain it for me? Thanks.

34)

2. public class Organic<E> {
3. void react(E e) {}
4. static void main(String[] args) {
5. //Organic<? extends Organic> compound = new Aliphatic<Organic>();
6. //Organic<? super Aliphatic> compound = new Aliphatic<Organic>();
7. compound.react(new Organic());
8. compound.react(new Aliphatic));
9. compound.react(new Hexane());
10. }}
11. class Aliphatic<F> extends Organic<F> {}
12. class Hexane<G> extends Aliphatic<G> {}

Which, taken independently, are true?
A. If line 5 is uncommented, compilation fails due to an error at line 7.
B. If line 5 is uncommented, compilation fails due to an error at line 8.
C. If line 5 is uncommented, compilation fails due to an error at line 9.
D. If line 6 is uncommented, compilation fails due to an error at line 7.
E. If line 6 is uncommented, compilation fails due to an error at line 8.
F. If line 6 is uncommented, compilation fails due to an error at line 9.


The answers are A,B,C, and D. As for why, I am totally lost in understanding the explanation.
 
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. This is a little tricky to understand so read slowly.

This code is all valid:

Nothing complex going on here. It shows that I can add any object or subclass to List<Object> but only a String to List<String>. If you aren't 100% comfortable so far, re-read or try it.

Now suppose I have the method


Still good. I can add any object to List<Object>.

Now suppose I have the method

I now have a compiler error. I *could* call the method with objectList. But I also *could* call it with stringList because List<? extends Object> can be any generic type. And the compiler is smart enough to know that I can call it with stringList (or any other type.) Which would cause list.add(new Object()) to blow up. The compiler gives an error rather than allowing this possibility to happen. Choices A, B and C are all equivalent to this scenario.

For D, the compiler thinks about what <? super Aliphatic> could be. It could be List<Aliphatic> or List<Organic>. Choices E and F are wrong because Aliphatic or Hexane can be added to either of these lists. Whereas choice D - Organic - can only be added to one. Again, it isn't guaranteed to be safe.
 
Joe Allen
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne,
Thanks for the great explanation you provided.
Based on your explanation of "For D, the compiler thinks about what <? super Aliphatic> could be. It could be List<Aliphatic> or List<Organic>. Choices E and F are wrong because Aliphatic or Hexane can be added to either of these lists. Whereas choice D - Organic - can only be added to one. Again, it isn't guaranteed to be safe. ", I now understand <? super Aliphatic> part.

As for <? extend (anything)> part, from what I understand, correct me if I am wrong, it means there is no fixed values that you can bind to it, therefore whenever we see <? extend anything>, any attempt to bind any value to it would cause compilor error.

I appreciate your help. You are great.
 
Bartender
Posts: 2438
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1. Consider

2. Consider :

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.

I am struggling with the same question. I cannot see what is the matter there. I would really appreciate if someone could help me.

I already read this answer (the third answer):
http://stackoverflow.com/questions/13133714/java-generics-super-vs-extends

But I can't get it yet. In that answer is written that:

Organic<? extends Organic> compound means that compound could be an Organic<SomeSubtypeOfHexane> (since Aliphatic extends Organic, Hexane extends Aliphatic and SomeSubtypeOfHexane extends Hexane).

Why? Does not Organic<? extends Organic> means any class which extends from Organic or Organic? Like Aliphatic or Hexane.

This one: Organic<? super Aliphatic> compound means that compount could be an Organic<Aliphatic>.

[color=darkblue]Again, why? Does not Organic<? super Aliphatic> means any class which is a super of Aliphatic or Aliphatic?. Like Organic.
[/color]
This has been a really hard question from K&B OCP Java SE 6 Programmer Practice Exams.
 
Himai Minh
Bartender
Posts: 2438
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Organic<? extends Organic> compound means compound is an Organic object, with E , which is an Organic or its subtype.
Organic<? super Organic> compound means compound is an Organic object, with E, which is an Organic or its supertype.

 
E Gonzalez
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:Organic<? extends Organic> compound means compound is an Organic object, with E , which is an Organic or its subtype.
Organic<? super Organic> compound means compound is an Organic object, with E, which is an Organic or its supertype.



Thanks Himai!. I was quite confused about that question. Now everything is clear!.
 
reply
    Bookmark Topic Watch Topic
  • New Topic