• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
Suguna Gollapally
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic