Back to Browse

How to Create Generic class, Generic function and Generic Interface in Kotlin? #generic #kotlin

19 views
Mar 16, 2026
1:02:51

Generics lets you write code that’s type-safe. Things you can do with a generic class or interface 1. Create a generified class. 2. Create a function that takes a generic type. 3. Create a function that returns a generic type. The generic type name can be anything that’s a legal identifier, but the convention (which you should follow) is to use “T”. The exception is if you’re writing a collection class or interface, in which case the convention is to use “E” instead (for “Element”), or “K” and “V” (for “Key” and “Value”) if it’s a map. You can restrict T to a specific supertype Use out to make a generic type covariant: If a generic type is covariant, it means that you can use a subtype in place of a supertype. Use in to make a generic type contravariant: in is the polar opposite of out. While out allows you to use a generic subtype in place of a supertype If a generic type is contravariant, it means that you can use a supertype in place of a subtype. This is the opposite of covariance. A generic type can be locally contravariant When a generic type has no in or out prefix, we say that the type is invariant. An invariant type can only accept references of that specific type. Telegram Group: https://t.me/androidNinjaWarrior Guide to watch: 00:30 Introduction of Generics in Kotlin 06:20 How to create a generic class in Kotlin? 11:30 How to create a generic function in Kotlin? 14:40 How to create a class with multiple generic type in Kotlin? 17:21 How to constrain generic type in Kotlin? 21:00 Kotlin Programming example of a generic class 40:58 How to create a generic interface in Kotlin? 45:19 covariance in Kotlin 54:00 contravariance in Kotlin 01:00:10 Revision of generics in Kotlin #Generics #Generic #GenericClass #GenericFunction #GenericInterface #constrainGeneric #Covariance #contravariance #in #out #kotlin #KotlinGeneric #GenericInKotlin Disclaimer- Some content are used for educational purposes under fair use. Copyright Disclaimer Under Section 107 of the Copyright Act 1976, allowance is made for "fair use" for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use. All credit for copyright material used in the video goes to the respected Owner. Thank you guys.

Download

0 formats

No download links available.

How to Create Generic class, Generic function and Generic Interface in Kotlin? #generic #kotlin | NatokHD