File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Generic method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generic method" Watch "Generic method" New topic

Generic method

Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
Posts: 112
Can someone explain me following code and why can't we have B as one of the answer ?

The author has given A, E and F as correct answers. I am bit confused about B. If we can accept <?> then why not <Object> ?
Henry Wong

Joined: Sep 28, 2004
Posts: 19676


Please Quote Your Sources.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
A Set<Orange> or a Set<Citrus> is not the same as a Set<Object>.
joko mujoko

Joined: Dec 28, 2006
Posts: 8
That's why we need to declare Set as a set of particular instance a class. Set<Object> is just like Set in Java 1.4.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 41994
To explain more about what Keith Lynn said. The fact that Citrus extends Object and Orange extends Citrus does not imply that a List<Orange> is a subclass of List<Citrus> or List<Orange>.

Go through the Generics part of the Java Tutorial, which explains why.
I agree. Here's the link:
subject: Generic method