wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help with understanding Generics question from OCP Java SE 6 Programmer Practice Exams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help with understanding Generics question from OCP Java SE 6 Programmer Practice Exams" Watch "Help with understanding Generics question from OCP Java SE 6 Programmer Practice Exams" New topic
Author

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

Jack McGuire
Greenhorn

Joined: Jan 24, 2012
Posts: 10
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

Joined: Jul 09, 2001
Posts: 1167
Right, A and B are correct. The compilation errors for the rest are -

C -



D -



E -



Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3253
    
    2
You might find this article useful


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
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

Joined: Nov 01, 2011
Posts: 451

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>();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with understanding Generics question from OCP Java SE 6 Programmer Practice Exams
 
Similar Threads
Generics
Generic method exercise
Doubt related to Generics Question
Ultimate generics Q
Generics understanding