WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Kotlin · Advanced · question 47 of 100

How do you use the Kotlin Reflection API to inspect and manipulate class instances at runtime? Provide an example.?

📕 Buy this interview preparation book: 100 Kotlin questions & answers — PDF + EPUB for $5

In Kotlin, reflection is used to inspect, annotate and manipulate classes, interfaces, properties, functions, constructors, parameters and fields during runtime. The Reflection API is a set of classes and interfaces that provide different ways to get information about the structure of your programs and perform operations on them. You can use these expressions to get an instance of a ‘KClass‘ or a ‘KCallable‘ object, which provides you with functionality to inspect and execute that class or function at runtime.

Here’s an example of how to use Kotlin Reflection API to inspect and manipulate the class instances at runtime:

import kotlin.reflect.full.memberProperties

data class Person(val name: String, val age: Int)

fun main() {
    val person = Person("John", 30)

    // Get the run-time type of an object
    val personClass = person::class

    // Inspect the properties of the class instance
    personClass.memberProperties.forEach { property ->
        println("${property.name} = ${property.get(person)}")
    }

    // Set the properties of the class instance
    personClass.memberProperties.first { it.name == "age" }.setter.call(person, 35)

    // Print the updated properties
    personClass.memberProperties.forEach { property ->
        println("${property.name} = ${property.get(person)}")
    }
}

In this example, we’re defining a simple ‘Person‘ data class with two properties - ‘name‘ and ‘age‘. Then, we’re creating an instance of the ‘Person‘ class with name "John" and age 30.

Next, we’re using the ‘::class‘ property to get the run-time type of the ‘person‘ object, which is ‘KClass<Person>‘. We can use this ‘personClass‘ object to inspect and manipulate the properties of the ‘person‘ object.

Then, we’re using the ‘memberProperties‘ extension function to get all the properties of the ‘Person‘ class. We’re iterating over each property and printing its name and value in the console.

Finally, we’re updating the ‘age‘ property of the ‘person‘ instance. We’re using the ‘setter‘ property of the ‘age‘ property to set its value to 35. We’re using the ‘call()‘ method to call the setter function with ‘person‘ object and ‘35‘.

In the end, we’re iterating over the properties again and printing their updated values in the console.

This is just a simple example, but as you can see, it demonstrates the basics of how we can use the Kotlin Reflection API to inspect and manipulate the class instances at runtime. The Reflection API provides many more capabilities beyond the ones shown here, such as calling functions, constructors, and inspecting annotations - you can explore these further in the Kotlin documentation.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Kotlin interview — then scores it.
📞 Practice Kotlin — free 15 min
📕 Buy this interview preparation book: 100 Kotlin questions & answers — PDF + EPUB for $5

All 100 Kotlin questions · All topics