Hello I have a class like this : ```class MyRespo...
# announcements
j
Hello I have a class like this :
Copy code
class MyResponse : ArrayList<MyResponseItem>
How do I create an object of MyResponse with a list of MyResponseItem?
I have a way, but I do not know if that's the correct/cleaner way... I've created a val like
Copy code
val myResponse = MyResponse().apply{addAll(responseItemList}
m
Copy code
class MyResponse(
    list: List<String> = emptyList()
) : ArrayList<String>(list){
    
}
Copy code
val myResponse = MyResponse(listOf("some", "string"))
j
Ty
j
In general, you should prefer composition over inheritance. So the advice above is correct, but ask yourself if you need a class at all or if you can just use
List<MyResponseItem>
directly.
👍 3
m
typealias is probably better 🙂
👍 1