File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics 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 "Generics" Watch "Generics" New topic
Author

Generics

Suguna Gollapally
Ranch Hand

Joined: Oct 31, 2006
Posts: 37
import java.util.*;
class shape { }
class Rect extends shape { }
class cir extends shape { }
class shade extends Rect{}
class Test {

public static void add(List<? extends shape>l,int pos,Rect r) {

l.add(pos,r);
}

public static void main(String ar[]) {

List<shade>=new ArrayList<shade>();
add(l,o,newshade());
}
}
What is the error in this program?? is this add() method..
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Suguna Gollapally:
import java.util.*;
class shape { }
class Rect extends shape { }
class cir extends shape { }
class shade extends Rect{}
class Test {

public static void add(List<? extends shape>l,int pos,Rect r) {

l.add(pos,r);
}

public static void main(String ar[]) {

List<shade>=new ArrayList<shade>();
add(l,o,newshade());
}
}
What is the error in this program?? is this add() method..


Because the parameter to the method accepts a List containing shape or anything that extends shape, there is no way to allow anything to be added to the List in the method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics
 
Similar Threads
Generics
Need help for importing classes
Generics doubt
A Paint Application with Combo Boxes and Buttons
Applet got refreshed