Which of the following statements CANNOT legally insert to the main method independently? (choose one)
A. new Letter<A>().getApplication(new Letter<A>());
B. new Letter<B>().getApplication(new Letter<B>());
C. new Letter<C>().getApplication(new Letter<C>());
D. new Letter<B>().getApplication(new Letter<C>());
E. new Letter<C>().getApplication(new Letter<B>());
F. new Letter().getApplication(new Letter());
G. new Letter().getApplication(new Letter<B>());
SCJP 5.0
A. new Letter<A>().getApplication(new Letter<A>());
//class Letter has type parameter "E extends B". Here A is the super class for B not subclass. So this line wont compile.
B. new Letter<B>().getApplication(new Letter<B>());
//B passes the test for class type parameter "E extends B". And the method getApplication's signature:
public <T extends E> Letter<? super T> getApplication(Letter<? super T> t)
here E is B and assume T as B: B satifies "? super T"
C. new Letter<C>().getApplication(new Letter<C>());
//C is the sub class of B so C passes "E extends B" for class type parameter. And assume T as C: C satifies "? super T"
D. new Letter<B>().getApplication(new Letter<C>());
// B passes the test for class type parameter "E extends B".
And assume T as C: C satifies "T extends B" and "? super T"
E. new Letter<C>().getApplication(new Letter<B>());
//C is the sub class of B so C passes "E extends B" for class type parameter. And assume T as C: B satifies "? super T"
F. new Letter().getApplication(new Letter());
//This will compile with warnings because of not using generics
G. new Letter().getApplication(new Letter<B>());
//This will also compile with warnings!
Thanks,<br />Srilatha M
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
F. new Letter().getApplication(new Letter());
G. new Letter().getApplication(new Letter<B>());
SCJP 5.0
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
G. new Letter().getApplication(new Letter<B> ());
here there is a possibility that the parameter passed to the method is of type B (read my explanation for this). Since no type is provided to new Letter() so the compiler would say
"I don't know the type of E (in class Letter<E extends B> ) , so I don't know the type of T (in <T extends E> ) , so beware, the actual type of the parameter that you are providing might not be B (in new Letter<B> ()). So you are responsible for anything wrong that happens"
As you know generics is only a compile time check so if the compiler thinks that the code is not safe, it warns you and it's up to you to decide what to do. If you continue with the warnings, then you loose the advantage of Generics type safety...
[ November 13, 2008: Message edited by: Ankit Garg ][/QB]
SCJP 5.0
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP 5.0
Author of ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- OCPJP Online Training -- Twitter -- How to Ask a Question
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Originally posted by suresh pilakal babu:
new Letter<B>().getApplication(new Letter<C>());
Hoe the test "E extends B" will pass here?
Here E is B, then it will become "B extends B".
"B extends B" is cyclic inheritance .right?
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Consider Paul's rocket mass heater. |