Ok, so here is my problem:
I have a list containing interfaces - List
- and a list of interfaces that extend that interface: Li
THIS WORK, BUT YOU SHOULD BE WARN !!!!
@Override
// Patient is Interface
public List getAllPatients() {
// patientService.loadPatients() returns a list of subclasess of Interface Patient
return (List)(List extends Patient>)patientService.loadPatients();
}
You can cast from List of Objects to List of Interface in this way.
But, if you get this list somewhere in your code and if you try to add something to this list, what would you add? Inteface or Subclass of this interface ? You actually loose information of the type of list, because you let it hold Interface, so you can add anything that implement this interface, but the list is holding the subclasses only, and you could easily get class cast exception if you try to do operations like add or get on this list with some other subclass. The solution is: Change The type of source list to list
instead of cast, then you are free to go :)