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 mixing Generics & non-generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "mixing Generics & non-generics" Watch "mixing Generics & non-generics" New topic

mixing Generics & non-generics

R Kumar

Joined: Jul 11, 2007
Posts: 6
Hi. The following code is taken from the javabeat mock exams.


1. import java.util.*;
2. public class Fruits {
3. public static void main(String [] args) {
4. Set c1 = new TreeSet();
5. Set o1 = new TreeSet();
6. bite(c1);
7. bite(o1);
8. }
9. // insert code here
10. }
11. class Citrus { }
12. class Orange extends Citrus { }

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

A). public static void bite(Set<?> s) { }
B). public static void bite(Set<Object> s) { }
C). public static void bite(Set<Citrus> s) { }
D). public static void bite(Set<? super Citrus> s) { }
E). public static void bite(Set<? super Orange> s) { }
F). public static void bite(Set<? extends Citrus> s) { }
G). Because of other errors in the code, none of these will compile.

According to me A,B,C,D,E,F are all correct

But the answer mentioned is A,E,F are correct. Could anyone please clarify?
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by R Kumar:
According to me A,B,C,D,E,F are all correct
You are correct. Since o1 and c1 have no element type information, the compiler assumes that the programmer knows what he does, and issues only a warning (except for case A, where any list is valid).
Robert Horvath

Joined: Jan 03, 2007
Posts: 15

You're absolutely right: for the given scenario the answers A,B,C,D,E,F are all correct.

If you'd start to insert code in method bite(), then you'd face limitations.
For example, change the bite()-method a little bit and add the command
s.add(new Citrus());

Now it becomes more interesting :-)

A. bite(Set<?> s) is NOT OK, because you could also pass a Set consisting of any type, i.e. also e.g. of String, thus you'd be not allowed to add a Citrus.

B. bite(Set<Object> s) is OK, because
you can add any subtype of Object to the set

C. bite(Set<Citrus> s) is OK, because
you can add any type of Citrus and any sub-type

D. bite(Set<? super Citrus> s) is OK, because
the Set must be of type Citrus or a super-type of Citrus, thus
it will be always possible to add a Citrus-object to the set.

E. bite(Set<? super Orange> s) is NOT OK, because
there could be e.g. an interface that is only implemented by class Orange,
thus it would not be allowed to add e.g. a Citrus.

F. bite(Set<? extends Citrus> s) is NOT OK, because
theoretically any type that extends Citrus is allowed, not only Citrus. One could pass a Set consisting of let's say Oranges, and then we couldn't longer add a Citrus-object.

Have fun playing around!
I agree. Here's the link:
subject: mixing Generics & non-generics