This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android 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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question on generic method
 
Similar Threads
Generics
binarySearch() ...when list is not sorted.
Comparator sort issue
why l.add() method is not working in this context?
Basic thread doubt