Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generic type instantiation question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generic type instantiation question" Watch "generic type instantiation question" New topic
Author

generic type instantiation question

Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
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.

"There are no dumb questions" quoted from HFSJ. "To err is human; to forgive is divine"
OCPJP 6, OCEJWCD 6
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

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.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
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.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 763


1. Consider

2. Consider :

E Gonzalez
Greenhorn

Joined: Mar 01, 2013
Posts: 2
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
Ranch Hand

Joined: Jul 29, 2012
Posts: 763
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

Joined: Mar 01, 2013
Posts: 2
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!.
 
GeeCON Prague 2014
 
subject: generic type instantiation question