Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic method

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

Please Quote Your Sources.

Thanks,
Henry
 
Keith Lynn
Ranch Hand
Posts: 2409
  • 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
  • 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
Posts: 48652
56
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic