This is my first time here, but I do my best to describe my problem:
I have an abstract class A
. I also have abstract classes B
and C
extending A
. Then I have classes extending B
or C
.
Now I have a generic class only accepting classes extending A
.
(class XYZ<ABC extends A>…
)
Now I basically want to create a method that works with the generic class, but only with instances that use a class extending B
.
I tried something like this:
public void method(XYZ<B> t){}
This sadly didn't work. The IDE wanted me to have either the type of t
or the other way around.
Now the questions:
Why does this not work? It seems that it wouldn't cause any problems, because a subclass has to provide all methods from the superclass.
Is there a way around this except of making a method for every single subclass of B or changing the type of the object i want to use?
example code:
public class main {
public static void main(String[] args) {
ArrayList<Abc> foo = new ArrayList<>();
xyz(foo);
}
private void xyz(ArrayList<B> abs){}
private static abstract class A{}
private static abstract class B extends A{}
private static abstract class C extends A{}
private static class Abc extends B{}
}