File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic method

 
Vivian Josh
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivian,

Please Quote Your Sources.

Thanks,
Henry
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Set<Orange> or a Set<Citrus> is not the same as a Set<Object>.
 
joko mujoko
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic