Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Parent {}
- public class Child extends Parent {}
- interface Store {
- public Set<Parent> getParents(); //PROBLEM!! This needs to change
- }
- Set<Parent> parents = store.getParents();
- parents.add(new Parent());
- parents.add(new Child());
- store.getParents().add(new Child()); //Note the lack of generics
- for(Parent curEntry : store.getParents()) {
- }
- interface Store {
- public Set<Parent> getParents();
- }
- class ConcreteStore implements Store {
- Set<Child> childs;
- public Set<Parent> getParents() {
- return (Set<? extends Parent>)childs; //ERROR: inconvertible types
- }
- }
- interface Store {
- public Set<? extends Parent> getParents();
- }
- class ConcreteStore implements Store {
- Set<Child> childs;
- public Set<Child> getParents() {
- return childs;
- }
- }
- Store store = new ConcreteStore();
- Set<? extends Parent> parents = store.getParents();
- parents.add(new Child()); //ERROR: cannot find symbol
- parents.add(new Parent()); //ERROR: cannot find symbol. What?!
- for (Parent curEntry : store.getParents()) {
- }
- interface Store<T extends Parent> {
- public Set<T> getParents();
- }
- public static class ConcreteStore implements Store<Child> {
- Set<Child> childs;
- @Override
- public Set<Child> getParents() {
- return childs;
- }
- }
- Store store = new ConcreteStore();
- Set<Parent> parents = store.getParents();
- parents.add(new Parent());
- parents.add(new Child());
- for (Parent curEntry : store.getParents()) { //ERROR: incompatible types. Found object, requited Parent
- }
- Set<Parent> parents = store.getParents();
- parents.add(new Parent());
- parents.add(new Child());
- store.getParents().add(new Child()); //Note the lack of generics
- for(Parent curEntry : store.getParents()) {
- }
- public class Parent {}
- public class Child extends Parent {}
- interface Store {
- public Set<Parent> getParents();
- }
- interface Store<X extends Parent> {
- public Set<X> getParents();
- }
- class ConcreteStore implements Store<Child> {
- Set<Child> childs;
- public Set<Child> getParents() {
- return childs;
- }
- }
- class ConcreteStore implements Store {
- Set<Parent> parents;
- public Set<Parent> getParents() {
- return parents;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement