Connect 4 Techs
  • Home
  • Excel
  • Data Analysis
  • Finance
  • MBA
    • Marketing
  • Kotlin
  • Gadgets
    • Apple
    • Samsung
    • Canon
    • Tech News
  • About
  • Contact
No Result
View All Result
Connect 4 Techs
  • Home
  • Excel
  • Data Analysis
  • Finance
  • MBA
    • Marketing
  • Kotlin
  • Gadgets
    • Apple
    • Samsung
    • Canon
    • Tech News
  • About
  • Contact
No Result
View All Result
Connect 4 Techs
No Result
View All Result
Home Kotlin

Kotlin Classes and Objects & Constructors (Lesson 12 PDF)

Ahmed Elhozayen by Ahmed Elhozayen
October 11, 2026
in Kotlin
0
Kotlin Classes and Objects & Constructors (Lesson 12 PDF)

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

You might also like

Kotlin Class Functions & Inheritance (Lesson 13 PDF)

Kotlin Functions & Classes (Lesson 11 PDF)

Kotlin For Loop & Kotlin Ranges (Lesson 10 PDF)

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 →

  1. Lesson 1: Getting Started
  2. Lesson 2: Syntax & Output
  3. Lesson 3: Comments & Variables
  4. Lesson 4: Data Types
  5. Lesson 5: Operators
  6. Lesson 6: Set Operations & Grouping
  7. Lesson 7: Booleans & If…Else
  8. Lesson 8: When, While, Break & Continue
  9. Lesson 9: Arrays
  10. Lesson 10: For Loops & Ranges
  11. Lesson 11: Functions & Classes
  12. Lesson 12: Classes, Objects & Constructors (you are here)
  13. 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:

From Here

Tags: AndroidJavaJVMKotlinProgramming
Previous Post

Kotlin Functions & Classes (Lesson 11 PDF)

Next Post

Kotlin Class Functions & Inheritance (Lesson 13 PDF)

Ahmed Elhozayen

Ahmed Elhozayen

Related Posts

Kotlin Class Functions & Inheritance (Lesson 13 PDF)
Kotlin

Kotlin Class Functions & Inheritance (Lesson 13 PDF)

by Ahmed Elhozayen
October 11, 2026
Kotlin Functions & Classes (Lesson 11 PDF)
Kotlin

Kotlin Functions & Classes (Lesson 11 PDF)

by Ahmed Elhozayen
October 11, 2026
Kotlin For Loop & Kotlin Ranges (Lesson 10 PDF)
Kotlin

Kotlin For Loop & Kotlin Ranges (Lesson 10 PDF)

by Ahmed Elhozayen
October 11, 2026
 Kotlin Arrays (Lesson 9 PDF)
Kotlin

 Kotlin Arrays (Lesson 9 PDF)

by Ahmed Elhozayen
October 11, 2026
Kotlin When & While Loop, Break & Continue (Lesson 8 PDF)
Kotlin

Kotlin When & While Loop, Break & Continue (Lesson 8 PDF)

by Ahmed Elhozayen
October 11, 2026
Next Post
Kotlin Class Functions & Inheritance (Lesson 13 PDF)

Kotlin Class Functions & Inheritance (Lesson 13 PDF)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

Apple's Vision Pro (and All of Mixed Reality) Needs to Keep Rethinking Our Hands

Apple’s Vision Pro (and All of Mixed Reality) Needs to Keep Rethinking Our Hands

April 7, 2024
Kotlin For Loop & Kotlin Ranges (Lesson 10 PDF)

Kotlin For Loop & Kotlin Ranges (Lesson 10 PDF)

October 11, 2026

Categories

  • apple
  • Canon
  • Data Analysis
  • Excel
  • Finance
  • ios
  • Kotlin
  • Marketing
  • MBA
  • Samsung
  • Software Tutorials
  • Technology

Don't miss it

20 Essential Excel Formulas Every Data Analyst Must Know in 2026
Data Analysis

20 Essential Excel Formulas Every Data Analyst Must Know in 2026

May 18, 2026
Excel guide for data analytics learners
Excel

Excel for Data Analysis PDF

October 11, 2026
The Ultimate Guide to Financial Modeling PDF
Excel

The Ultimate Guide to Financial Modeling PDF

October 11, 2026
The Ultimate Excel Handbook PDF
Excel

The Ultimate Excel Handbook PDF

October 11, 2026
𝟓𝟎 𝐄𝐱𝐜𝐞𝐥 𝐒𝐡𝐨𝐫𝐭𝐜𝐮𝐭 𝐊𝐞𝐲
Excel

50 Excel Shortcut Key PDF: Streamline Your Workflow

March 21, 2025
Most Useful Excel Keyboard Shortcuts For Windows and Mac OS
Excel

Most Useful Excel Keyboard Shortcuts for Windows and Mac OS PDF

March 21, 2025
Connect4Techs Tech

Practical, free guides on Excel, data analysis, finance, business and programming — written to help you learn faster and work smarter.

Follow on Facebook

Topics

  • Excel
  • Kotlin
  • MBA
  • Finance
  • Data Analysis
  • Apple
  • Technology
  • Software Tutorials

Latest articles

  • 20 Essential Excel Formulas Every Data Analyst Must Know in 2026May 18, 2026
  • Excel for Data Analysis PDFAug 31, 2025
  • The Ultimate Guide to Financial Modeling PDFApr 19, 2025

Company

  • About us
  • Contact
  • Privacy Policy
  • Terms of Use
  • IT Certification PDFs
© 2026 Connect4Techs. All rights reserved. Privacy · Terms · Contact
No Result
View All Result
  • Home
  • Excel
  • Data Analysis
  • Finance
  • MBA
    • Marketing
  • Kotlin
  • Gadgets
    • Apple
    • Samsung
    • Canon
    • Tech News
  • About
  • Contact