• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Generics

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic