9

I think I made some mistake while writing my code. I have a class MyClass that implements Interface, now I've a method that is generic for List of Interface as a parameter. I want to use my method by passing a List. I can't cast from List to List (that I assumed I could). So, what can I do to make my code work? here an example:

List<MyClass> lista = returnMyClassList();
myMethod((List<Interface>) lista); //this doesn't work

//myMethod signature
public void myMethod(List<Interface> struttura);

Thanks for helping.

Pievis
  • 1,954
  • 1
  • 22
  • 42

5 Answers5

13

Use an upper bound of Interface for the type: <? extends Interface>.

Here's come compilable code that uses classes from the JDK to illustrate:

public static void myMethod(List<? extends Comparable> struttura) {}

public static void main(String[] args) {
    List<Integer> lista = null;
    myMethod(lista); // compiles OK
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
2

You can use:

public void myMethod(List<? extends Interface> struttura);

In order to get both List<Interface> and List<MyClass>

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
2

List<MyClass> is not a sub type of List<Interface> you are getting error.
Accorind to java docs

Given two concrete types A and B (for example, Number and Integer), MyClass has no relationship to MyClass, regardless of whether or not A and B are related. The common parent of MyClass and MyClass is Object

So Change your method signature to

   public void myMethod(List<? extends Interface> struttura){
   }

and call the method

   List<MyClass> lista = returnMyClassList();      
   myMethod((lista);
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
2

You should use public void myMethod(List<? extends Interface> struttura);. This way you will be able to pass any type that IS-A Interface. You would like to read about Polymorpshism and generics

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
1

You cannot cast this, because you would make a promise you cannot hold.

When you cast from List<MyClass> to List<Interface> you take a list containing several objects of type MyClass and vast it to a type that makes (among others) the following promises:

  1. Interface get(int index) - you can get an element of thpe Interface (no problem)
  2. boolean add(Interface e) - you can add any element of type Interface (if it is a subtype of MyClass or not) (this is a problem!)

The second point is problematic, because the underlying List is still a list of MyClass and not a list of Interface in general. You could still hold a link to the underlying List in a separate variable and could run get on that list. If you added an object of type Interface, that is not also a MyClass, you could get this Non-MyClass from the List<MyClass>.

As all the other answers and comments have pointed out, the correct solution is the type List<? extends Interface>. With this type, you can still get objects of type Interface from the list (as a return value of a method call), but you cannot put anything into the list (use as a parameter of a method call).

stefan.schwetschke
  • 8,862
  • 1
  • 26
  • 30