aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 103

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: 9280
    
  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: 103
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
Generics
Comparator sort issue
Basic thread doubt
why l.add() method is not working in this context?
binarySearch() ...when list is not sorted.