Casting between ArrayLists in Java

后端 未结 6 643
故里飘歌
故里飘歌 2020-12-15 11:56

Sorry, I thought this was an inheritance question: it was an ArrayList question all along!

Ok, my problem is more specific than I thought. So I have two families of

6条回答
  •  没有蜡笔的小新
    2020-12-15 12:42

    First of all, it's usually better practice to use getter/setter methods than accessing properties directly, especially if you're going to be doing a lot of complicated inheritance.

    As for the generics problem, you could try defining the cardBox getter in the superclass (or top-level interface/abstract class) as:

    protected ArrayList getCardBox();
    

    That way you can ovverride it in subclasses and have them return a list of any type of subclass of Card.

提交回复
热议问题