0

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{}
}
Wondestarc
  • 13
  • 3
  • What error do you get? Please show all code necessary to reproduce that error. See [minimal, complete example](http://stackoverflow.com/help/mcve). – Radiodef Jun 02 '16 at 00:12
  • uh, sry, late here:"[...] to either change the type of t the the objects type or[...]" – Wondestarc Jun 02 '16 at 00:12
  • 1
    I can't reproduce the problem. Can you post some actual code? – shmosel Jun 02 '16 at 00:16
  • Here's an example on Ideone which follows your description and compiles: http://ideone.com/0vE3N3. So again, per my earlier comment, please update your question with a more clear code example. – Radiodef Jun 02 '16 at 00:17
  • @Radiodef The method is supposed to be inside `class XYZ` as far as I understood it. – RaminS Jun 02 '16 at 00:19
  • @Gendarme doesn't matter. – shmosel Jun 02 '16 at 00:20
  • Added some code and yes, my English grammar knowledge is not the best. – Wondestarc Jun 02 '16 at 00:24
  • Sry, I can't compile it per console on this pc, but eclipse tells me that I should either change the method to private void xyz(ArrayList abs){} or foo to ArrayList – Wondestarc Jun 02 '16 at 00:29
  • 1
    see if this answers your question: http://stackoverflow.com/q/2745265/2891664 (possible duplicate) – Radiodef Jun 02 '16 at 00:35
  • This will help: https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html In short: `Abc` is a subclass of `B`, but `ArrayList` is not a subclass of `ArrayList`. – RaminS Jun 02 '16 at 00:35
  • Yeah, I just found the solution by the related stuff, that i couldnt find the last 2 hours... well, thanks anyways and sry for stealing your time :( – Wondestarc Jun 02 '16 at 00:36

1 Answers1

1

Try it like 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 to be what you are aiming for. Take a look here for more information.