Companion Objects in Kotlin
Companion objects in Kotlin are used to define static members and methods for a class. Here's a list of questions and interview questions related to companion objects, along with examples and explanations:
1. What is a companion object in Kotlin?
A companion object is a singleton object associated with a class. It is used to define static members and methods for the class.
2. How do you declare a companion object in Kotlin?
Kotlin
Copied
3. What is the purpose of a companion object?
A companion object provides a way to define static members and methods that are associated with the class itself rather than instances of the class.
4. Can you access companion object members without creating an instance of the class?
Yes, companion object members can be accessed using the class name without creating an instance.
5. Explain the use of companion object properties in Kotlin.
Kotlin
Copied
6. How can you call a companion object method from outside the class?
Kotlin
Copied
7. What is the difference between a companion object and a regular object in Kotlin?
A companion object is associated with a class, and its members can access the class's private members, while a regular object is a standalone singleton.
8. How do you define a custom name for a companion object?
Kotlin
Copied
9. Explain the concept of extension functions on companion objects.
Kotlin
Copied
10. What is the purpose of the @JvmStatic
annotation in Kotlin companion objects?
@JvmStatic
is used to generate static methods in the Java bytecode for better interoperability.
11. How do you create a singleton using a companion object in Kotlin?
Kotlin
Copied
12. Explain the use of by lazy
with companion objects in Kotlin.
Kotlin
Copied
13. Can a companion object implement an interface in Kotlin?
Yes, a companion object can implement an interface, making it possible to define common functionality for multiple classes.
14. How do you use companion objects in the context of object-oriented design patterns?
Companion objects are often used to implement factory methods, builder patterns, or other design patterns where class-level methods are required.
15. Explain the role of companion objects in providing alternative constructors.
Kotlin
Copied
16. What is the purpose of the companion
keyword in Kotlin?
The companion
keyword is used to declare a companion object, and it is optional when accessing its members.
17. How can you reference a companion object from Java code?
Companion object members can be accessed using the class name and the Companion
suffix in Java.
Kotlin
Copied
18. Explain the use of companion objects in providing constant values.
Kotlin
Copied
19. What is the significance of a companion object extension in Kotlin?
A companion object extension allows adding new functionality to a companion object outside its original declaration.
20. How do you ensure that a companion object property is initialized only once?
Kotlin
Copied
Explanation:
The
myProperty
property is declared aslateinit
to allow for lazy initialization.The
initializePropertyOnce
function uses a synchronized block to ensure that only one thread at a time can check and initialize the property.Inside the synchronized block, it checks if the property is not initialized, then initializes it with the provided value.
The
getProperty
function checks if the property is initialized before returning its value. If it's not initialized, anIllegalStateException
is thrown.In the
main
function, we callinitializePropertyOnce
with different values, but only the first call initializes the property. The subsequent calls won't reinitialize it.Finally, we retrieve and print the current value of the property using
getProperty
.
This ensures that the companion object property is initialized only once, even in a multithreaded environment, thanks to the use of synchronization.