Advanced Kotlin Class Features

May 22 2024 · Kotlin 1.9, Android 14, Android Studio Hedgehog

Lesson 01: Define Generic Classes


Episode complete

Play next episode


Open your internet browser and search for Kotlin Playground. You’ll see different websites. Select the one provided by the Android Developers page. This web-based compiler allows you to write Kotlin code and compile it to see the output on the same page.

First, define a class called FruitBox. This class has four variables with different data types:

class FruitBox<A, B, C : Number, D : Number>(
  private val firstItem: A,
  private val secondItem: B,
  private val numOfItems: C,
  private val totalCost: D
) {

Here’s a code breakdown:

  1. class FruitBox<A, B, C : Number, D : Number> declares a generic class named FruitBox with four type parameters: A, B, C, and D.
  2. private val firstItem: A and private val secondItem: B: These two private variables store the two items in the FruitBox, each of generic types A and B.
  3. private val numOfItems: C is a private variable representing the number of items in the FruitBox. Type C is constrained to be a subtype of a Number.
  4. private val totalCost: D is another private variable representing the total cost of the items in the FruitBox. Type D is constrained to be a subtype of Number.

Now that you’ve declared the variables you need, declare a generic function within the FruitBox to print the output of those variables to the console.

Create a new function called printContents with the following code:

fun <A, B, C : Number, D : Number> printContents(fruitBox: FruitBox<A, B, C, D>) {
  println("FruitBox contents: First Item = ${fruitBox.firstItem}, Second Item = ${fruitBox.secondItem}")
  println("Total Items = ${fruitBox.numOfItems}, Total Cost = $${fruitBox.totalCost}")

Here’s a code breakdown:

  1. <A, B, C : Number, D : Number> are the generic type parameters for the function. They help you specify different types for the properties of the FruitBox instance passed to the function.
  2. A and B are generic types for the first and second items in the FruitBox, while C and D are generic types constrained to be subclasses of Number, used for the number of items and total cost in the FruitBox.
  3. FruitBox<A, B, C, D> specifies the parameter FruitBox. You use the println statements to display the details about the first and second items, the total number of items, and the cost.

Running the code right now won’t display anything to the console because you haven’t yet called the printContents function from the main function. Create instances of the FruitBox and use the printContents in the main function to print the output to the console:

fun main() {
  val appleBananaBox = FruitBox("Apple", "Banana", 5, 10.5)

Here’s a breakdown of the code above:

  1. val appleBananaBox = FruitBox("Apple", "Banana", 5, 10.5) creates an instance of FruitBox with specific items, Apple and Banana, a quantity of 5, and a total cost of 10.5.
  2. appleBananaBox.printContents() Prints out the content of the box to the console.

Now, run the code to see the output to the console.

See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion