I have an interface A like this:
public interface A{
void myFirstMethod();
void mySecondMethod();
}
And then I have this class:
public class MyClass{
private List<? extends A> elements;
public MyClass(){
A obj = new A(){
@Override
public void myFirstMethod(){
//SOME CODE
}
@Override
public void mySecondMethod(){
//SOME CODE
}
};
elements.add(obj);
}
}
I haven't use generics before (only things like List<String>...
) so I can't see why this code doesn't compile. To be more precise, I get an error on the line elements.add(obj);
that the method add is not applicable for these parameters.
EDIT: I've changed the code and now the elements.add(obj) compiles fine, but I have another problem.
public class MyClass{
private List<A> elements;
public MyClass(List<A> elements){
this.elements = elements;
elements.add(obj);
}
}
When I try to do this, it doesn't compile.
//A1 implements A
List<A1> list = new ArrayList<A1>();
MyClass(list);
How could I fix this?
Imagine that you have two subclasses:
class A1 implements A {...}
class A2 implements A {...}
Then you could write:
List<? extends A> elements = new ArrayList<A1>(); // a list of A1
But then this should not compile:
elements.add(new A2()); //oops: an A2 is not an A1
For that reason, you can't add anything but null
to a List<? extends A>
because you don't know what actual generic type it is. In other words, ? extends A
means a specific, but unknown, subtype of A
(or A
itself).
In your case, a List<A>
would probably do what you expect - you would be able to add some A1
s and some A2
s to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With