File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java Generics 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 "Java Generics" Watch "Java Generics" New topic

Java Generics

Jae Lim

Joined: Feb 20, 2009
Posts: 12
Hello All,
I am going over mock up exams and having hard time understanding crazy generic on method signatures. This is one of the problem from the mock up exam. I am trying to understand why each each of the following method declarations are right/wrong. I am also curious what would be the best strategies to solve this type of problem in prompt manner. Is drawing the UML diagram a way to go? I can't possibly imagine spending like 10-20 on this problem I looked at the explanation but it wasn't all covered for all choices. If any of you can help me understand this better or possibly suggest any external resources, I will sincerely appreciate it.

Given the following class declaration:

abstract class A<K extends Number>{
// Insert Here

Which are the valid method declarations, for the above class?

A) public abstract <K> K useMe(Object k);

B) public abstract K<K> useMe(Object k);

C) public abstract <K> A<? extends Number> useMe(A<? super K> k);

D) public abstract <K> A<? super Number> useMe(A<? extends K> k);

E) public abstract <V extends K> A<V> useMe(A<V> k);

F) public abstract <V extends K> A<V> useMe(A<V> k);

G) public abstract <V super K> A<V> useMe(A<V> k);

H) public abstract <V extends Character> A<? super V> useMe(A<K> k);

I) public abstract <V super Character> A<? super V> useMe(A<K> k);

Also, if you have a method signature like the following:
public <T> Pair<T,T> twice (T value)


Documentation says that you can add the parameters can be added like the following:
Pair<String, String> pair = this.<String>twice("Hello");

I know <T> on the method above defines return type. Can someone please explain me this funky syntax? What does <T> do when placed in front of the method invokation?



Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Jae Lim wrote:This is one of the problem from the mock up exam.

Please Quote Your Sources and Use Code Tags. It is a rule here.
I agree. Here's the link:
subject: Java Generics
It's not a secret anymore!