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
- Keep class properties private.
- 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
- Mark base classes as
opento allow inheritance. - Use
overrideexplicitly 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
- Prefer polymorphism to conditional logic for flexibility.
- 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
- Use abstract classes for shared implementation.
- 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.