In Kotlin, constructors are used to initialize an object of a class. There are two types of constructors in Kotlin: the **Primary Constructor** and the **Secondary Constructor**. Let's explore both in detail!
The primary constructor is part of the class header and is a concise way to initialize properties. It can also include initialization code.
// Defining a class with a primary constructor class Person(val name: String, var age: Int) { // Initialization code can also be included inside init block init { println("Person created: $name, Age: $age") } } // Creating an object val person1 = Person("Alice", 25)
In this example, name is a read-only property (val), and age is a mutable property (var). The init
block is executed when an object is created, and it allows you to add initialization logic.
The secondary constructor is declared inside the class body. It's useful when you need more flexibility for object initialization or need to provide multiple ways to create an object.
// Defining a class with a secondary constructor class Car(val make: String, val model: String) { var year: Int // Secondary constructor constructor(make: String, model: String, year: Int) : this(make, model) { this.year = year } fun displayInfo() { println("Car: $make $model, Year: $year") } } // Creating an object using the secondary constructor val myCar = Car("Honda", "Civic", 2022) myCar.displayInfo() // Output: Car: Honda Civic, Year: 2022
In this case, we have a secondary constructor that initializes the year property in addition to the primary properties. The secondary constructor calls the primary constructor using this()
and adds extra initialization.
Kotlin allows constructor overloading, meaning you can have multiple constructors with different parameter sets to provide various ways to create objects. Let's take a look:
// Constructor overloading example class Book(val title: String, val author: String) { var price: Double // Primary constructor constructor(title: String, author: String, price: Double) : this(title, author) { this.price = price } fun displayBookInfo() { println("Book: $title by $author, Price: $price") } } // Creating objects with different constructors val book1 = Book("Kotlin Programming", "John Doe") val book2 = Book("Learn Kotlin", "Jane Smith", 29.99) book1.displayBookInfo() // Output: Book: Kotlin Programming by John Doe, Price: 0.0 book2.displayBookInfo() // Output: Book: Learn Kotlin by Jane Smith, Price: 29.99
Here, the Book class has two constructors. The first constructor initializes only the title and author, while the second constructor adds an optional price property.
You can also provide default values for constructor parameters. This allows you to create objects without passing all arguments explicitly.
// Constructor with default values class Laptop(val brand: String = "Dell", val model: String = "XPS 13", val price: Double = 999.99) { fun displayInfo() { println("Laptop: $brand $model, Price: $$price") } } // Creating objects with default values val laptop1 = Laptop() // Uses default values val laptop2 = Laptop("Apple", "MacBook Pro", 2399.99) laptop1.displayInfo() // Output: Laptop: Dell XPS 13, Price: $999.99 laptop2.displayInfo() // Output: Laptop: Apple MacBook Pro, Price: $2399.99
The Laptop class allows you to use default values for properties. If no value is provided, the default value is used.
Here are some challenges for you to practice:
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!