1

I tried:

ArrayList<Pelicula> peliculas = YIFY.obtenerPeliculasPorVenir();

being #obtenerPeliculasPorVenir:

public static List<Pelicula> obtenerPeliculasPorVenir(){

        List peliculas = null;

        try {
            peliculas = mapper.readValue(new API().peticionTexto("http://yts.re/api/upcoming.json"), new TypeReference<List<Pelicula>>(){});
        }

        catch (IOException excepcion) {
            System.out.println(excepcion.getMessage());
        }

        return peliculas;
    }
}

If ArrayList implements List why can't I do this?

Is casting the ONLY solution or I should go for another OOP approach?

diegoaguilar
  • 8,179
  • 14
  • 80
  • 129
  • Because a List is not an ArrayList, it could be a LinkedList. You can only assign a subtype to a supertype, not a supertype to a subtype as you are trying to do here. – ggovan May 07 '14 at 15:25

1 Answers1

8

Because any ArrayList is a List but not all Lists are ArrayLists, for example LinkedList.

Is casting the ONLY solution or I should go for another OOP approach?

NO. The best bet is to always code to a high level interface/abstract class:

List<Pelicula> peliculas = YIFY.obtenerPeliculasPorVenir();

More info:


When should you use downcasting?

When the framework only provides access to the data as higher level classes. For example, in Java Web Development, retrieving an attribute from the session through HttpSession

//example to validate if user is logged
HttpSession session = request.getSession();
User loggedUser = (User)session.getAttribute("user"); //it returns Object
if (loggedUser == null) {
    //there's no user logged in!
    //do something about it!
}
//the user is logged, he/she can continue working...
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • So, `peliculas` will be always a List with *not* the ArrayList only or any other class implementing it aditional methods? What when I want to treat peliculas exactly as an `ArrayList` or a `LinkedList`? – diegoaguilar May 07 '14 at 15:26
  • You're becoming StackOverflow rich with this newbie question, great :P :D – diegoaguilar May 07 '14 at 15:27
  • 1
    @diegoaguilar why would you want to treat it as a specific class to begin with? Usually, in real world applications, you don't code/design like that (unless you want to get fired). But there are very few specific cases when you need to do the downcasting, and downcasting is explicit, but not for this case. – Luiggi Mendoza May 07 '14 at 15:28
  • Ok, I MUST accept I still need to mature and grow up in this treats, I even understand the *"unless you want to get fired"* comment. I'd like to start using wisely interfaces along with Inheritance – diegoaguilar May 07 '14 at 15:31
  • @diegoaguilar Another interesting thing that you can look up is something called `composition over inheritance`. It's a rather interesting idea. – awksp May 07 '14 at 15:34
  • @user3580294 can you prompt a nice answer giving an example with my case? – diegoaguilar May 07 '14 at 15:35
  • @diegoaguilar It's just something that I thought I might mention because you mentioned inheritance; I don't think it's actually relevant to this specific question. The rule of thumb for composition over inheritance is to think whether a proposed subtype has a `has-a` relationship with its supertype (`Bird` and `Airplane` might both *have a* `fly()` method), or whether the subtype has an `is-a` relationship (`JumboJet` *is an* `Airplane`). – awksp May 07 '14 at 15:41
  • @user3580294 you should decide if you will use composition over inheritance depending on the current problem you have/need to solve, but that's not what OP asks. OP question is: why `ArrayList` cannot be directly assigned from a `List` variable that, in fact, is an `ArrayList`. And yes, I agree that composition is better than inheritance in many facts. Also, from your last comment, not all birds can fly e.g. penguin. – Luiggi Mendoza May 07 '14 at 15:44
  • @LuiggiMendoza I know it wasn't OP's question, I admitted it wasn't, and I explained why I mentioned it -- because he mentioned inheritance. There is a reason why I said "rule of thumb"; it's a guideline more than an absolute rule, and you still need to use your judgement. Yes, there are counterexamples, but my example wasn't really intended to be comprehensive. My apologies if that wasn't clear. – awksp May 07 '14 at 15:51