Kotlin classes & Objects


What is a Class?

A class defines a pattern to hold variables and related methods.

Think of it as a recipe for building items.

class Laptop {     
     var brand = "Dell"     
     var price = 800 
} 

What is an Object?

An object is a real version made from a class.

It holds its own values and can use the class's logic.

val myLaptop = Laptop() 
Println(myLaptop.brand) 

Using Functions in Classes

You can include actions inside classes using functions.

class Dog {     
     fun bark() {         
             println("Woof!")    
      } 
}
Val pet = Dog() 
pet.bark() 

Primary Constructor

Used to initialize an object with external info when it's created.

class Book(val title: String, val pages: Int) 
Val myBook = Book("Kotlin Basics", 200) 

init Block

A special block that runs immediately when an object is made.

class Phone(val model: String) {    
           init {         
                println("$model is powered on.")     
          } 
} 

Access Modifiers

Used to restrict or allow visibility of class members.

  • private – only within the class
  • protected – accessible in subclass
  • internal – visible in same module
  • public – visible everywhere (default)

Prefer Learning by Watching?

Watch these YouTube tutorials to understand KOTLIN Tutorial visually:

What You'll Learn:
  • 📌 Kotlin - Classes and Objects
  • 📌 #4 Kotlin Tutorial | Class | Object
Previous Next