Companion Objects

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:

Codecastic
0
Codecastic
0
Codecastic
0
Codecastic
0

On this page

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?

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.

Copied


6. How can you call a companion object method from outside the class?

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?

Copied


9. Explain the concept of extension functions on companion objects.

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?

Copied


12. Explain the use of by lazy with companion objects in 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.

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.

Copied


18. Explain the use of companion objects in providing constant values.

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?

Copied

Explanation:

  1. The myProperty property is declared as lateinit to allow for lazy initialization.

  2. The initializePropertyOnce function uses a synchronized block to ensure that only one thread at a time can check and initialize the property.

  3. Inside the synchronized block, it checks if the property is not initialized, then initializes it with the provided value.

  4. The getProperty function checks if the property is initialized before returning its value. If it's not initialized, an IllegalStateException is thrown.

  5. In the main function, we call initializePropertyOnce with different values, but only the first call initializes the property. The subsequent calls won't reinitialize it.

  6. 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.

Last updated -

Share this page

On this page
Companion Objects in Kotlin
1. What is a companion object in Kotlin?
2. How do you declare a companion object in Kotlin?
3. What is the purpose of a companion object?
4. Can you access companion object members without creating an instance of the class?
5. Explain the use of companion object properties in Kotlin.
6. How can you call a companion object method from outside the class?
7. What is the difference between a companion object and a regular object in Kotlin?
8. How do you define a custom name for a companion object?
9. Explain the concept of extension functions on companion objects.
10. What is the purpose of the @JvmStatic annotation in Kotlin companion objects?
11. How do you create a singleton using a companion object in Kotlin?
12. Explain the use of by lazy with companion objects in Kotlin.
13. Can a companion object implement an interface in Kotlin?
14. How do you use companion objects in the context of object-oriented design patterns?
15. Explain the role of companion objects in providing alternative constructors.
16. What is the purpose of the companion keyword in Kotlin?
17. How can you reference a companion object from Java code?
18. Explain the use of companion objects in providing constant values.
19. What is the significance of a companion object extension in Kotlin?
20. How do you ensure that a companion object property is initialized only once?