superclasses in generic classes in java -
this first time here, best describe problem:
i have abstract class a
. have abstract classes b
, c
extending a
. have classes extending b
or c
.
now have generic class accepting classes extending a
. (class xyz<abc extends a>…
)
now want create method works generic class, instances use class extending b
.
i tried this:
public void method(xyz<b> t){}
this sadly didn't work. ide wanted me have either type of t
or other way around.
now questions:
why not work? seems wouldn't cause problems, because subclass has provide methods superclass.
there way around except of making method every single subclass of b or changing type of object want 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{} }
try this:
public class main { public static void main(string[] args) { arraylist<abc> foo = new arraylist<>(); xyz(foo); } private static <t extends b> void xyz(arraylist<t> abs){ system.out.print("running xyz"); } 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{} }
seems aiming for. take here more information.