This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with understanding Generics question from OCP Java SE 6 Programmer Practice Exams

 
Jack McGuire
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the question - Self Assessment 2 Question 11.

Given:
3. class A { }
4. class B extends A { }
5. class C extends B { }
6. public class Carpet<V extends B> {
7. public <X extends V> Carpet<? extends V> method(Carpet<? super X> e) {
8. // insert code here
9. } }

Which, inserted independently at line 8, will compile? (Choose all that apply.)

A. return new Carpet<X>();
B. return new Carpet<V>();
C. return new Carpet<A>();
D. return new Carpet<B>();
E. return new Carpet<C>();

Here is the answer (for Objective 6.4) provided in the book:

A and B are correct. The generic declaration at the class level says that Carpet can
accept any type which is either B or a subtype of B. The generic declaration at the method
level is "<X extends V>", which means that X is a type of V or a subtype of V, where
the class type of V can vary at runtime—hence, the exact scopes of X and V are unknown
at compile time. A and B are correct because X and V bind to the scope of <? extends
V>, where X is known as a subtype of V as it’s declared at the method level.

C, D, and E are incorrect because it’s illegal to use a concrete class type since the exact
scope of V is unknown.

While I feel I understand simple Generics, I have no idea about how to deduce an answer
for this question. Any help that clarifies this answer would be most appreciated.

Thanks
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, A and B are correct. The compilation errors for the rest are -

C -



D -



E -



Regards,
Dan
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3760
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find this article useful
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my tip for you.
When dealing with questions regarding to generic method , you will be asked about the return type with wild card and the actual return.
This is a typical question.
Return type Carpet<? extends V>. What should be returned ? Should we return Carpet <B> ? Should we return Carpet <C> ?
It is ok to write this in main method or any other method:
Carpet<? extends V> c = new Carpet<B>

But when we deal with return type and actual return, my tip for you is not to consider any concrete type for the return in this situation.
When the compiler sees your method

static<X extends V> Carpet<? extends V> get (Carpet <? super B> e) {
return ______________;
}


The compiler will say : I don't know what exactly V will be. V extends B. V can be B or C. Should I return Carpet<B> or Carpet<C> to you? The best I can do for you is :
return you Carpet<V> as Carpet <? extends V> is a super type of Carpet<V>. (For more detail, please refer to Mughal and Rasmussen's book chapter 13 Generics, Partial type hierarchy).
Or, since X is anything extends V, X can substitute V. Therefore, I can return you Carpet<X>.

 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

One situation that the actual return type can be concrete is this:
public <X extends V> Carpet< ? > method(Carpet<? super X> e) {
return new Carpet<Object>();
}


The compiler says: Hey, this method can return a Carpet with any parameter type extending Object ( ? is equivalent to ? extends Object) . In the type hierachy tree, Carpet<?> c = Carpet <Object> or
List<?> l = new ArrayList<Object>();
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic