📖 Scenario: Imagine you are building a library system that can hold collections of different types of items, like books, magazines, or DVDs. Sometimes, you don't know exactly what type of items a collection holds, but you still want to work with it safely.
🎯 Goal: You will create a Kotlin program that uses star projection to handle collections with unknown types. This helps you work with generic types safely when you don't know the exact type inside.
📋 What You'll Learn
Create a generic class called Box that holds a value of any type.
Create a list of Box objects with different types inside.
Use star projection Box<*> to handle the list with unknown types.
Print the contents of each Box safely using star projection.
💡 Why This Matters
🌍 Real World
Star projection helps when working with libraries or APIs where you don't know the exact generic type but still want to use the data safely.
💼 Career
Understanding star projection is important for Kotlin developers working with generics, especially in Android development and library design.
Progress0 / 4 steps
1
Create the generic class Box
Create a generic class called Box with a property value of type T. This class will hold any type of value.
Kotlin
Hint
Use class Box<out T>(val value: T) to create a generic class with a value property.
2
Create a list of Box objects with different types
Create a list called boxes that contains Box objects holding these exact values: Box(123), Box("Hello"), and Box(45.6).
Kotlin
Hint
Use listOf(Box(123), Box("Hello"), Box(45.6)) to create the list.
3
Use star projection to handle unknown types
Create a variable called unknownBoxes of type List<Box<*>> and assign it the value of boxes. This uses star projection to handle boxes with unknown types.
Kotlin
Hint
Use val unknownBoxes: List<Box<*>> = boxes to declare the variable with star projection.
4
Print the contents of each Box safely
Use a for loop with variable box to iterate over unknownBoxes. Inside the loop, print the value property of each box.
Kotlin
Hint
Use for (box in unknownBoxes) { println(box.value) } to print each value.
Practice
(1/5)
1. What does the star projection * represent in Kotlin generics?
easy
A. A type that can only be used with primitive data types
B. A specific generic type that must be defined explicitly
C. A wildcard that allows writing but restricts reading
D. An unknown generic type that allows safe reading but restricts writing
Solution
Step 1: Understand star projection meaning
The star projection * means an unknown generic type in Kotlin.
Step 2: Analyze its usage
It allows safe reading from generic collections but restricts writing to avoid type errors.
Final Answer:
An unknown generic type that allows safe reading but restricts writing -> Option D
Quick Check:
Star projection = unknown generic type with safe read [OK]
Hint: Star projection means unknown type, safe read only [OK]
Common Mistakes:
Thinking star projection allows writing to generics
Confusing star projection with specific generic types
Assuming star projection only works with primitives
2. Which of the following is the correct syntax to declare a list with star projection in Kotlin?
easy
A. val list: List<Any> = listOf(1, 2, 3)
B. val list: List<*> = listOf(1, 2, 3)
C. val list: List<> = listOf(1, 2, 3)
D. val list: List = listOf(1, 2, 3)
Solution
Step 1: Recall star projection syntax
Star projection uses * inside angle brackets to represent unknown type.
Step 2: Check each option
val list: List<*> = listOf(1, 2, 3) uses List<*> which is correct syntax for star projection.