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
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: 9305
    
  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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Question on generic method