This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread 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 Murach's Java Servlets and JSP this week in the Servlets 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: 9291
    
  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
 
Similar Threads
binarySearch() ...when list is not sorted.
Comparator sort issue
why l.add() method is not working in this context?
Generics
Basic thread doubt