fork download
  1.  
  2. import java.util.List;
  3.  
  4. import java.util.ArrayList;
  5.  
  6.  
  7.  
  8.  
  9. public class Main {
  10.  
  11.  
  12. public static void main(String[] args) {
  13.  
  14. // test addLot
  15. {
  16. List<Asset> assets = new ArrayList<Asset>();
  17. addLot(assets);
  18.  
  19. List<Lot> lots = new ArrayList<Lot>();
  20. addLot(lots);
  21.  
  22. List<CommercialLot> commercialLots = new ArrayList<CommercialLot>();
  23. // you cannot do this, compile-time error:
  24. // addLot(commercialLots);
  25. }
  26.  
  27. // test addCommercialLot
  28. {
  29. List<Asset> assets = new ArrayList<Asset>();
  30. addCommercialLot(assets);
  31.  
  32. List<Lot> lots = new ArrayList<Lot>();
  33. addCommercialLot(lots);
  34.  
  35. List<CommercialLot> commercialLots = new ArrayList<CommercialLot>();
  36. addCommercialLot(commercialLots);
  37. }
  38.  
  39. }
  40.  
  41. public static void addLot(List<? super Lot> lots) {
  42.  
  43. // you cannot do this, compile-time error:
  44. // lots.add(new Asset());
  45.  
  46. // you can do this:
  47. lots.add(new Lot());
  48.  
  49. // and so is this
  50. lots.add(new CommercialLot());
  51.  
  52. ////////
  53.  
  54. for(Asset a : (List<Asset>) lots) {
  55. }
  56.  
  57. for(Lot l : (List<Lot>) lots) {
  58. }
  59.  
  60. // you cannot do this, compile-time error:
  61. /*for(CommercialLot c : (List<CommercialLot>) lots) {
  62.   } */
  63. }
  64.  
  65. public static void addCommercialLot(List<? super CommercialLot> commercialLots) {
  66.  
  67. // you cannot do this, compile-time error:
  68. // commercialLots.add(new Asset());
  69.  
  70. // you cannot do this, compile-time error:
  71. // commercialLots.add(new Lot());
  72.  
  73. // but of course, you can do this:
  74. commercialLots.add(new CommercialLot());
  75.  
  76. ////////
  77.  
  78. for(Asset a : (List<Asset>) commercialLots) {
  79. }
  80.  
  81. for(Lot l : (List<Lot>) commercialLots) {
  82. }
  83.  
  84. for(CommercialLot c : (List<CommercialLot>) commercialLots) {
  85. }
  86. }
  87. }
  88.  
  89. class Asset {
  90. }
  91.  
  92. class Lot extends Asset {
  93. }
  94.  
  95. class CommercialLot extends Lot {
  96. }
  97.  
  98.  
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
Standard output is empty