Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM). It was developed by JetBrains and is now an official language for Android development. Kotlin is designed to be concise, expressive, and safe. It is also interoperable with Java, which means that you can use Kotlin and Java code together in the same project.
Kotlin Classes and Objects & Constructors (Lesson 12 PDF)
Introduction
Welcome to the 12th lesson of our Kotlin tutorial series. In this lesson, we will learn about classes and objects in Kotlin, as well as how to use constructors to initialize them. Classes and objects are fundamental concepts in object-oriented programming, and Kotlin provides a concise and powerful syntax to work with them.
Overview about the Lesson
This lesson covers the following topics:
- How to declare a class and create an object from it
- How to define properties and methods for a class
- How to use primary and secondary constructors to initialize a class
- How to use inheritance and polymorphism to reuse and extend existing classes
- How to use abstract classes and interfaces to define common behaviors for different classes
- How to use data classes and enum classes to represent simple data types




The Content of the Lesson
The lesson consists of a PDF file that contains detailed explanations, examples, exercises, and solutions for each topic. You can download the PDF file from the link at the end of this post. You can also watch the video version of the lesson on our YouTube channel.
Why This Lesson
Learning about classes and objects is essential for any Kotlin developer, as they are the building blocks of any application. By mastering these concepts, you will be able to create complex and modular programs that are easy to maintain and extend.
You will also be able to use existing libraries and frameworks that rely on object-oriented principles.
Kotlin Classes, Objects and Constructors: Examples
This lesson’s PDF explains classes, objects and constructors. Here is a compact, practical tour with code you can run.
A class with a primary constructor
class Car(val brand: String, val model: String, var year: Int)
fun main() {
val car = Car("Toyota", "Corolla", 2022)
println("${car.brand} ${car.model} (${car.year})") // Toyota Corolla (2022)
car.year = 2023 // var property: can change
// car.brand = "Kia" // error: val property is read-only
}Declaring val or var in the constructor creates a property automatically. No separate fields, getters or setters are needed.
init blocks and validation
class Account(val owner: String, initialBalance: Double) {
var balance = initialBalance
private set // can only be changed inside the class
init {
require(initialBalance >= 0) { "Balance cannot be negative" }
}
fun deposit(amount: Double) {
balance += amount
}
}
fun main() {
val acc = Account("Nour", 100.0)
acc.deposit(50.0)
println(acc.balance) // 150.0
}The init block runs when an object is created, which makes it the right place for validation. private set lets everyone read the balance but only the class change it.
Default values instead of many constructors
class User(val name: String, val role: String = "viewer", val active: Boolean = true)
fun main() {
val a = User("Ali")
val b = User("Mona", role = "admin")
println("${a.role} ${b.role}") // viewer admin
}Secondary constructors
class Rectangle(val width: Int, val height: Int) {
constructor(side: Int) : this(side, side) // a square
fun area() = width * height
}
fun main() {
println(Rectangle(3, 4).area()) // 12
println(Rectangle(5).area()) // 25
}A secondary constructor must call the primary one with this(...). In practice, default values cover most cases, so secondary constructors are less common in Kotlin than in Java.
Data classes
data class Product(val name: String, val price: Double)
fun main() {
val p1 = Product("Mouse", 15.0)
val p2 = Product("Mouse", 15.0)
println(p1) // Product(name=Mouse, price=15.0)
println(p1 == p2) // true: compares content
val p3 = p1.copy(price = 12.0)
println(p3) // Product(name=Mouse, price=12.0)
}data class generates toString, equals, hashCode and copy for you. Use it for classes whose job is to hold data.
Practice exercise
Create a Book class with a title, an author and a page count, plus a function that says whether the book is long (more than 300 pages).
class Book(val title: String, val author: String, val pages: Int) {
fun isLong() = pages > 300
}
fun main() {
val b = Book("Kotlin Basics", "A. Writer", 320)
println("${b.title} is long: ${b.isLong()}") // Kotlin Basics is long: true
}Frequently Asked Questions
Does Kotlin use the new keyword?
No. Create objects by calling the constructor like a function: val car = Car("Toyota", "Corolla", 2022).
When should I use a data class?
When the class mainly stores values (API responses, database rows, settings). Regular classes suit objects that mainly have behaviour.
Continue the course
← Previous: Lesson 11: Functions & Classes | Next: Lesson 13: Class Functions & Inheritance →
- Lesson 1: Getting Started
- Lesson 2: Syntax & Output
- Lesson 3: Comments & Variables
- Lesson 4: Data Types
- Lesson 5: Operators
- Lesson 6: Set Operations & Grouping
- Lesson 7: Booleans & If…Else
- Lesson 8: When, While, Break & Continue
- Lesson 9: Arrays
- Lesson 10: For Loops & Ranges
- Lesson 11: Functions & Classes
- Lesson 12: Classes, Objects & Constructors (you are here)
- Lesson 13: Class Functions & Inheritance
Conclusion
We hope you enjoy this lesson and find it useful for your Kotlin journey. If you have any questions or feedback, please leave a comment below or contact us via email. Don’t forget to subscribe to our newsletter and follow us on social media for more Kotlin tutorials and resources.
Download from Link
You can download the lesson from the following link:













