© Peter Späth 2019
Peter SpäthLearn Kotlin for Android Developmenthttps://doi.org/10.1007/978-1-4842-4467-8_13

13. About Type Safety: Generics

Peter Späth1 
(1)
Leipzig, Germany
 
Generics is a term used for denoting a set of language features that allow us to add type parameters to types. Consider, for example, a simple class with a function for adding elements in the form of Int objects:
class AdderInt {
    fun add(i:Int) {
        ...
    }
}
and another one for String objects:
class AdderString {
    fun add(s:String) {
        ...
    }
}
Apart from what happens inside the add() function, these classes look suspiciously similar, so we could think of a language feature that abstracts the type for the element to add. Such a language feature exists in Kotlin, ...

Get Learn Kotlin for Android Development: The Next Generation Language for Modern Android Apps Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.