Classes and Objects
1. What is a class in Kotlin?
A class is a blueprint for creating objects. It defines properties and behaviors that the objects created from the class will have.
2. How do you define a class in Kotlin?
Use the class
keyword followed by the class name.
Copied
3. What is an object in Kotlin?
An object is an instance of a class. It represents a real-world entity and has its own unique state.
4. How do you create an instance of a class in Kotlin?
Use the val
or var
keyword followed by the instance name and the class
constructor.
Copied
5. Explain the difference between a class and an object.
A class is a blueprint or template for creating objects, while an object is an instance of a class.
6. What are properties in Kotlin classes?
Properties are the characteristics or attributes of a class. They define the state of an object.
Copied
7. What are methods in Kotlin classes?
Methods are functions defined within a class. They represent the behavior of the objects created from the class.
Copied
8. How is inheritance implemented in Kotlin?
Use the :
symbol followed by the name of the superclass after the subclass declaration.
Copied
9. Explain the concept of visibility modifiers in Kotlin.
Visibility modifiers (private
, protected
, internal
, public
) control the visibility of classes, properties, and methods.
Copied
10. What is the primary constructor in Kotlin?
The primary constructor is declared in the class header. It defines the properties of the class.
Copied
11. How do you create secondary constructors in Kotlin?
Use the constructor
keyword along with this
to define secondary constructors.
Copied
12. What is a data class in Kotlin?
A data class is a class specifically designed for holding data. It automatically generates equals()
, hashCode()
, toString()
, and copy()
methods.
Copied
13. Explain the init
block in Kotlin.
The init
block is executed when an instance of the class is created. It is used for additional initialization.
Copied
14. What is the purpose of the companion object
in Kotlin?
The companion object
is used to create static members and methods in a class.
Copied
15. Explain the difference between val
and var
in the context of class properties.
val
is used for read-only (immutable) properties, and var
is used for mutable properties.
Copied
16. How does Kotlin handle method overloading?
Method overloading is supported in Kotlin by defining multiple methods with the same name but different parameter lists.
Copied
17. What is an abstract class in Kotlin?
An abstract class cannot be instantiated and may contain abstract (unimplemented) methods. Subclasses must provide implementations.
Copied
18. How is a sealed class different from a regular class in Kotlin?
A sealed class is a limited hierarchy where all subclasses are known. It is often used in when
expressions to ensure exhaustive checks.
Copied
19. What is the purpose of the by lazy
delegate in Kotlin?
The by lazy
delegate is used for lazy initialization of properties. It computes the value only when the property is accessed for the first time.
Copied
20. Explain the object
keyword in Kotlin.
The object
keyword is used to define a singleton object – a class that can have only one instance.
Copied