Generics

suggest change

Introduction

A List can hold numbers, words or really anything. That’s why we call the List generic.

Generics are basically used to define which types a class can hold and which type an object currently holds.

Syntax

Parameters

| Parameter | Details | | ––––––– | —————————— | | TypeName | Type Name of generic parameter | | UpperBound | Covariant Type | | LowerBound | Contravariant Type | | ClassName | Name of the class |

Remarks

Implied Upper Bound is Nullable

In Kotlin Generics, the upper bound of type parameter T would be Any?. Therefore for this class:

class Consumer<T>

The type parameter T is really T: Any?. To make a non-nullable upper bound, explicitly specific T: Any. For example:

class Consumer<T: Any>

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents