Kotlin Classes & Objects

Kotlin Tutorial

Kotlin classes and objects are the foundation of Object-Oriented Programming (OOP).
Kotlin improves on Java by providing concise syntax, safety, and powerful features.


1. Class Basics

Simple Class


Creating an Object



2. Primary Constructor (Most Important)

class Person(val name: String, var age: Int)
  • val → read-only property

  • var → mutable property

Usage:

val p = Person("Rahul", 25)

3. Init Block

Executed when the object is created.



4. Secondary Constructor

Used for alternative initialization.


 


5. Properties & Custom Getters/Setters


field → backing field


6. Visibility Modifiers

ModifierScope
publicEverywhere (default)
privateSame file/class
protectedClass & subclasses
internalSame module

7. Methods in Classes


8. this Keyword

Refers to the current object.


9. Inheritance Basics


 


10. Abstract Classes


11. Interfaces


12. Data Classes (Deep)

Features:

  • copy()

  • componentN() (destructuring)

  • Auto equals() & hashCode()


13. Object Declaration (Singleton)


14. Companion Object (Static-like)


15. Anonymous Objects


16. Sealed Classes (Brief)


17. Real-World Example


 


Summary

✔ Concise constructors
✔ Properties + methods together
✔ Less boilerplate than Java
✔ Strong OOP + functional mix

You may also like...