In Kotlin, constructors are used to initialize objects when a class is instantiated. Kotlin provides two types of constructors:
- Primary Constructor
- Secondary Constructor
1. Primary Constructor
- The primary constructor is part of the class header and is used to initialize properties of the class.
- It is concise and typically used when a class has straightforward initialization needs.
Syntax:
class ClassName(param1: Type, param2: Type) {
// Initialization block (if needed)
init {
// Code to initialize or process properties
}
}
Example:
class Person(val name: String, var age: Int) {
// `init` block to add additional initialization logic
init {
println("Name: $name, Age: $age")
}
}
// Create an instance
val person = Person("Alice", 25)
Explanation:
val name: String
andvar age: Int
in the primary constructor automatically create and initialize properties.- The
init
block executes when the object is created.
2. Secondary Constructor
- A secondary constructor is defined inside the class body using the
constructor
keyword. - It provides alternative ways to instantiate the class when additional initialization logic is required or the primary constructor doesn’t fit.
Syntax:
class ClassName {
// Secondary constructor
constructor(param1: Type, param2: Type) {
// Code for initialization
}
}
Example:
class Person {
var name: String
var age: Int
// Secondary constructor
constructor(name: String, age: Int) {
this.name = name
this.age = age
println("Name: $name, Age: $age")
}
}
// Create an instance
val person = Person("Bob", 30)
Explanation:
- The secondary constructor explicitly initializes the properties
name
andage
. - It can contain custom initialization logic specific to the secondary constructor.
Combining Primary and Secondary Constructors
When both primary and secondary constructors are present:
- Secondary constructors must delegate to the primary constructor (directly or indirectly) using the
this
keyword.
Example:
class Person(val name: String, var age: Int) {
var city: String = "Unknown"
// Secondary constructor delegating to the primary constructor
constructor(name: String, age: Int, city: String) : this(name, age) {
this.city = city
}
init {
println("Primary constructor: Name: $name, Age: $age")
}
}
// Create instances
val person1 = Person("Charlie", 40) // Uses primary constructor
val person2 = Person("Dave", 35, "New York") // Uses secondary constructor
println(person2.city) // Output: New York
Explanation:
- The primary constructor initializes
name
andage
. - The secondary constructor adds initialization for
city
and ensures delegation to the primary constructor using: this(name, age)
.
Key Differences Between Primary and Secondary Constructors
Feature | Primary Constructor | Secondary Constructor |
---|---|---|
Definition Location | In the class header. | Inside the class body. |
Purpose | Simplifies initialization of properties. | Provides alternative ways to create objects. |
Delegation | Cannot delegate to secondary constructors. | Must delegate to the primary constructor. |
Use Cases | Simple and common initialization. | Complex initialization or alternative setups. |
When to Use Which?
- Use primary constructors for straightforward property initialization (preferred for most cases).
- Use secondary constructors when:
- You need multiple ways to initialize the class.
- Complex initialization logic is required.
😊
Leave a Reply