Chapter 10: Classes & Objects – Basics

Classes & Objects – Basics — this is the chapter where we finally start building real-world things in Kotlin! 🎉

Kotlin’s class system is much cleaner, safer, and more concise than Java’s — you’ll write less boilerplate, get null safety, and enjoy primary constructors that make everything feel elegant.

Imagine we’re sitting together in a quiet Bandra café — I’ve got my laptop open, and I’m going to teach you classes like I’m explaining it to my younger brother who just discovered how powerful OOP can be when it’s done the Kotlin way.

We’ll go super slowly, with lots of real-life analogies, complete runnable programs, step-by-step explanations, tables, common mistakes with fixes, and plenty of examples you can copy-paste and run right now.

Let’s dive in!

1. Class Declaration – The Basics

Syntax (the simplest possible class):

Kotlin

That’s it! No public, no braces, no semicolon — just class Person.

But usually we add properties (fields) and constructors:

Kotlin

2. Primary Constructor – The Most Important Part

Kotlin has one primary constructor — it’s declared right after the class name in parentheses.

Features:

  • Can have parameters with val or var → automatically becomes property
  • Can have default values
  • Can have visibility modifiers (private, protected, etc.)

Example 1 – Primary constructor with val/var

Kotlin

Key points:

  • val name → read-only property (getter auto-generated)
  • var age → read-write property (getter + setter auto-generated)

3. Secondary Constructors

You can have zero or more secondary constructors — they must call the primary constructor (directly or indirectly).

Syntax:

Kotlin

Example 2 – Secondary constructor

Kotlin

4. Properties (val/var) – The Heart of Kotlin Classes

Properties are class members that have:

  • Backing field (optional)
  • Getter (always)
  • Setter (only for var)

Three ways to declare properties:

  1. In primary constructor (most common)
Kotlin
  1. Inside class body (when you need custom getter/setter)
Kotlin
  1. Computed properties (no backing field)
Kotlin

Example – Full class with properties

Kotlin

Usage:

Kotlin

5. init Blocks – Initialization Logic

init blocks run every time an object is created — right after primary constructor.

Most common uses:

  • Validation
  • Logging
  • Complex initialization
Kotlin

Output when creating object:

text

6. Member Functions & Fields – Putting It All Together

Complete real-world class example

Kotlin

Usage:

Kotlin

Output:

text

Quick Recap Table (Your Cheat Sheet)

Feature Kotlin Way (Best Practice) Key Point
Primary constructor class Person(val name: String, var age: Int) Parameters become properties automatically
Secondary constructor constructor(name: String) : this(name, 18) Must call primary constructor
Properties in body var age: Int = 18 + custom getter/setter Use for validation/computation
Computed property val age: Int get() = … No backing field
init block init { … } Runs after primary constructor
Private setter var balance: Double private set Only class can change value

Common Newbie Mistakes & Fixes

Mistake Problem Fix
Writing public class Person public not needed – default is public Just write class Person
Declaring fields outside constructor Verbose & error-prone Use primary constructor parameters
Not using val/var in constructor Parameter not a property Always write val name: String or var
Multiple init blocks – wrong order Logic runs in declaration order Be careful – order matters
Forgetting this in secondary constructor Compile error Always call this(…)

Homework for You (Let’s Make It Fun!)

  1. Basic Create a Book class with val title, val author, var price (with validation: price >= 0).
  2. Medium Add a secondary constructor to Book that takes only title and author (price = 500.0 default).
  3. Advanced Create a Rectangle class with val length, val width → computed properties area and perimeter.
  4. Fun Make a Person class with val name, var age, and init block that prints birthday message if age < 18.
  5. Challenge Create a BankAccount class with private balance, deposit(), withdraw() (with validation), and displayBalance().

You’ve just built your first real Kotlin classes — you’re now officially doing OOP the Kotlin way!

You may also like...

Leave a Reply

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