Advertisement
Guest User

Untitled

a guest
Jul 9th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. public class Parent {}
  2. public class Child extends Parent {}
  3. interface Store {
  4. public Set<Parent> getParents(); //PROBLEM!! This needs to change
  5. }
  6.  
  7. Set<Parent> parents = store.getParents();
  8. parents.add(new Parent());
  9. parents.add(new Child());
  10. store.getParents().add(new Child()); //Note the lack of generics
  11.  
  12. for(Parent curEntry : store.getParents()) {
  13.  
  14. }
  15.  
  16. interface Store {
  17. public Set<Parent> getParents();
  18. }
  19.  
  20. class ConcreteStore implements Store {
  21. Set<Child> childs;
  22. public Set<Parent> getParents() {
  23. return (Set<? extends Parent>)childs; //ERROR: inconvertible types
  24. }
  25. }
  26.  
  27. interface Store {
  28. public Set<? extends Parent> getParents();
  29. }
  30.  
  31. class ConcreteStore implements Store {
  32. Set<Child> childs;
  33. public Set<Child> getParents() {
  34. return childs;
  35. }
  36. }
  37.  
  38. Store store = new ConcreteStore();
  39. Set<? extends Parent> parents = store.getParents();
  40. parents.add(new Child()); //ERROR: cannot find symbol
  41. parents.add(new Parent()); //ERROR: cannot find symbol. What?!
  42.  
  43. for (Parent curEntry : store.getParents()) {
  44. }
  45.  
  46. interface Store<T extends Parent> {
  47. public Set<T> getParents();
  48. }
  49.  
  50. public static class ConcreteStore implements Store<Child> {
  51. Set<Child> childs;
  52.  
  53. @Override
  54. public Set<Child> getParents() {
  55. return childs;
  56. }
  57. }
  58.  
  59. Store store = new ConcreteStore();
  60. Set<Parent> parents = store.getParents();
  61. parents.add(new Parent());
  62. parents.add(new Child());
  63.  
  64. for (Parent curEntry : store.getParents()) { //ERROR: incompatible types. Found object, requited Parent
  65. }
  66.  
  67. Set<Parent> parents = store.getParents();
  68. parents.add(new Parent());
  69. parents.add(new Child());
  70. store.getParents().add(new Child()); //Note the lack of generics
  71.  
  72. for(Parent curEntry : store.getParents()) {
  73.  
  74. }
  75.  
  76. public class Parent {}
  77. public class Child extends Parent {}
  78. interface Store {
  79. public Set<Parent> getParents();
  80. }
  81.  
  82. interface Store<X extends Parent> {
  83. public Set<X> getParents();
  84. }
  85.  
  86. class ConcreteStore implements Store<Child> {
  87. Set<Child> childs;
  88. public Set<Child> getParents() {
  89. return childs;
  90. }
  91. }
  92.  
  93. class ConcreteStore implements Store {
  94. Set<Parent> parents;
  95. public Set<Parent> getParents() {
  96. return parents;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement