This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on generic method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on generic method" Watch "Question on generic method" New topic
Author

Question on generic method

ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105

Why does the add(new B()) line doesnt work when the list is of type A ?


SCJP 6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

You know that the list is of type A, but the go method can be called with any type of object. Suppose if you call go method with an instance of class C or if I call the go method with an instance of Integer class, then what?? Your call to go method will work at runtime, you know that, but the compiler has to make sure that there is no way the go method can be called in such a way that it breaks type safety...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
Yes that makes sense..Thanks Ankit I understood..
This generics syntaxes are very confusing
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on generic method