Kotlin OOP

Kotlin Tutorial

Object-Oriented Programming (OOP) in Kotlin is based on objects and classes. Kotlin fully supports OOP principles and adds modern features that make code cleaner, safer, and more concise than Java.


1. Core OOP Concepts in Kotlin

It follows 4 main OOP principles:

  1. Class & Object

  2. Encapsulation

  3. Inheritance

  4. Polymorphism


2. Class & Object

Creating a Class


Creating an Object


 


3. Constructors

Primary Constructor


Usage:


Secondary Constructor


 


4. Encapsulation

Encapsulation means hiding internal data and exposing only what is necessary.


 

Access modifiers:

  • public (default)

  • private

  • protected

  • internal


5. Inheritance

open Keyword

Classes are final by default in Kotlin.


Child Class



6. Polymorphism

Same function name, different behavior.



7. Abstract Classes



8. Interfaces



9. Data Classes

Used to hold data only.

data class User(val name: String, val age: Int)

Automatically provides:

  • toString()

  • equals()

  • hashCode()

  • copy()


10. Object Keyword (Singleton)


11. Companion Object (Static)


12. Sealed Classes

Used for restricted class hierarchies.


Summary

  • It fully supports OOP

  • Classes are final by default

  • Less boilerplate than Java

  • Powerful features like data, sealed, object

You may also like...