← Back to Kotlin Course | Chapter 11: Generics | Lesson 1 of 6

Generic Classes क्या हैं

एक generic class एक container blueprint है जो 'अंदर किस तरह की चीज़ जाती है' के लिए एक खाली जगह छोड़ता है, बाद में भरने के लिए।
Syntax
kotlin
class ClassName<T>(val item: T) {
    fun get(): T = item
}

val a = ClassName(value)               // T inferred
val b = ClassName<Type>(value)         // explicit type argument

Declaring a Generic Class

class name के बाद angle brackets में एक type parameter, जैसे <T>, उस class को किसी भी type के साथ काम करने देता है, जहां भी इसके अंदर उपयोग हो वहां T के रूप में भरा जाता है।

उदाहरण: Declaring a Generic Class

markup
// Define a class `Box`
class Box<T>(val item: T)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `intBox`, set to `Box(42)`
    val intBox = Box(42)
    // Declare a read-only variable `stringBox`, set to `Box("Kotlin")`
    val stringBox = Box("Kotlin")
    // Print "${intBox.item}, ${stringBox.item}" to the console, with a trailing newline
    println("${intBox.item}, ${stringBox.item}")
}

Type Inference for Generics

Kotlin आमतौर पर constructor को पास की गई value से type argument को अपने आप infer कर लेता है, इसलिए आपको शायद ही कभी explicitly Box<Int> लिखने की ज़रूरत पड़े।

उदाहरण: Type Inference for Generics

markup
// Define a class `Pair2`
class Pair2<A, B>(val first: A, val second: B)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `pair`, set to `Pair2("age", 30)`
    val pair = Pair2("age", 30)
    // Print "${pair.first} = ${pair.second}" to the console, with a trailing newline
    println("${pair.first} = ${pair.second}")
}

Explicit Type Arguments

जब compiler के पास infer करने के लिए कुछ न हो, जैसे एक खाली generic container बनाना, type argument को angle brackets में explicitly प्रदान करना चाहिए।

उदाहरण: Explicit Type Arguments

markup
// Define a class `Container`
class Container<T> {
    private val items = mutableListOf<T>()
    fun add(item: T) = items.add(item)
    // Define the function `all` with no parameters and returning a `List<T>`
    fun all(): List<T> = items
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `container`, set to `Container<String>()`
    val container = Container<String>()
    // Add `"Kotlin"` to `container`
    container.add("Kotlin")
    // Add `"Generics"` to `container`
    container.add("Generics")
    // Print `container.all()` to the console, with a trailing newline
    println(container.all())
}

Multiple Type Parameters

एक generic class एक से ज़्यादा type parameters declare कर सकती है, इसे एक साथ कई स्वतंत्र placeholder types के साथ काम करने देते हुए।

उदाहरण: Multiple Type Parameters

markup
// Define a class `KeyValue`
class KeyValue<K, V>(val key: K, val value: V) {
    fun describe() = "$key -> $value"
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `entry`, set to `KeyValue("name", "Kotlin")`
    val entry = KeyValue("name", "Kotlin")
    // Print `entry.describe()` to the console, with a trailing newline
    println(entry.describe())
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक generic Box<T> के बजाय इसे जिस हर type को रखना चाहिए उसके लिए एक अलग class लिखना (जैसे IntBox, StringBox)।
  2. जब type infer न किया जा सके तब type argument specify करना भूल जाना, जैसे infer करने के लिए बिना किसी elements के एक खाली generic collection बनाना।
  3. type parameter name (जैसे T) को एक असली type के साथ confuse करना; T बस एक placeholder नाम है, conventionally एक single capital letter।
चैप्टर सारांश
  • एक generic class अपने नाम के बाद angle brackets में एक या ज़्यादा type parameters declare करती है, जैसे class Box<T>(val item: T)।
  • type parameter T एक placeholder की तरह काम करता है जो class उपयोग होने पर एक concrete type से replace हो जाता है।
  • Type arguments अक्सर constructor arguments से अपने आप infer होते हैं, लेकिन ज़रूरत पड़ने पर explicitly specify किए जा सकते हैं।
  • Generic classes हर अलग type के लिए जिसे इन्हें रखना हो एक जैसा logic दोहराने से बचाती हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.