Kotlin Object-Oriented Programming Concepts: Encapsulation, Inheritance, Polymorphism, and Abstraction


This Kotlin OOP Concepts tutorial explains the four core principles of object-oriented programming: encapsulation, inheritance, polymorphism, and abstraction. Each concept is explained with practical Kotlin examples and best practices to help beginners and professionals write clean, modular, and reusable object-oriented Kotlin code for real-world applications.

Kotlin OOP Concepts – Complete Tutorial

Encapsulation

Encapsulation is the practice of restricting access to the internal state of an object and providing controlled access via methods (getters and setters).

Example


class BankAccount {
private var balance: Double = 0.0

fun deposit(amount: Double) {
if (amount > 0) balance += amount
}

fun withdraw(amount: Double) {
if (amount > 0 && amount <= balance) balance -= amount
}

fun getBalance(): Double {
return balance
}
}

fun main() {
val account = BankAccount()
account.deposit(1000.0)
account.withdraw(500.0)
println("Balance: ${account.getBalance()}")
}

Best Practices

  1. Keep class properties private.
  2. Provide controlled access via functions or custom getters/setters.

Inheritance

Inheritance allows one class to acquire the properties and methods of another class, promoting code reuse.

Syntax


open class Vehicle(val brand: String) {
open fun drive() {
println("$brand is driving")
}
}

class Car(brand: String, val model: String) : Vehicle(brand) {
override fun drive() {
println("$brand $model is driving fast")
}
}

fun main() {
val car = Car("Toyota", "Corolla")
car.drive()
}

Best Practices

  1. Mark base classes as open to allow inheritance.
  2. Use override explicitly for overridden methods.

Polymorphism

Polymorphism allows objects to take multiple forms, primarily through method overriding and interfaces.

Example: Method Overriding


open class Animal {
open fun sound() = println("Some sound")
}

class Dog : Animal() {
override fun sound() = println("Bark")
}

fun main() {
val animal: Animal = Dog()
animal.sound() // Output: Bark
}

Example: Interface Polymorphism


interface Flyable {
fun fly()
}

class Bird : Flyable {
override fun fly() = println("Bird is flying")
}

fun main() {
val flyable: Flyable = Bird()
flyable.fly()
}

Best Practices

  1. Prefer polymorphism to conditional logic for flexibility.
  2. Use interfaces for multiple behaviors.

Abstraction

Abstraction hides implementation details and exposes only relevant features.

Abstract Class Example


abstract class Shape {
abstract fun area(): Double
}

class Circle(private val radius: Double) : Shape() {
override fun area() = 3.14 * radius * radius
}

fun main() {
val circle = Circle(5.0)
println("Area of circle: ${circle.area()}")
}

Interface Example


interface Drawable {
fun draw()
}

class Rectangle : Drawable {
override fun draw() = println("Drawing a rectangle")
}

Best Practices

  1. Use abstract classes for shared implementation.
  2. Use interfaces for pure abstraction or multiple inheritance.

Summary

This chapter explained the four pillars of object-oriented programming in Kotlin: encapsulation, inheritance, polymorphism, and abstraction. Applying these concepts effectively helps create modular, reusable, and maintainable code in Kotlin applications.