👩💻 Object-Oriented Programming (OOP) in Kotlin – Classes, Inheritance, Interfaces & More
🧲 Introduction – Build Reusable, Scalable Code with Kotlin’s OOP Features
Kotlin is a fully object-oriented language that enhances Java’s OOP capabilities with modern syntax, null safety, and concise code. It allows developers to create classes, define behavior through methods, and structure applications using inheritance, abstraction, and interfaces.
Kotlin’s class model is simple yet powerful, enabling reuse, extension, and encapsulation of functionality for cleaner, maintainable software.
🎯 In this guide, you’ll learn:
- How to define classes, constructors, and methods in Kotlin
- How to implement inheritance and override behavior
- How to use abstract classes and interfaces effectively
- How to manage encapsulation with visibility modifiers
📘 Topics Covered
📦 Topic | 📖 Description |
---|---|
🧠 Kotlin – OOP Basics | Principles of object-oriented design and how Kotlin simplifies them. |
🏗️ Kotlin – Classes and Objects | Creating class blueprints and instantiating objects. |
🚪 Kotlin – Constructors | Defining primary and secondary constructors with default and custom parameters. |
🧮 Kotlin – Class Functions | Adding behavior to classes using member functions and properties. |
👪 Kotlin – Inheritance | Reusing code using class hierarchies and method overriding. |
🧩 Kotlin – Abstract Classes | Declaring base classes with partial implementations and abstract members. |
🧷 Kotlin – Interfaces | Defining contracts with methods and properties without implementation. |
🔐 Kotlin – Visibility Modifiers | Controlling access with public , private , protected , and internal . |
🔍 Detailed Sections & Examples
🧠 Kotlin – OOP Basics
OOP in Kotlin is based on four key principles:
- Encapsulation – Grouping related logic in classes
- Abstraction – Hiding internal implementation
- Inheritance – Reusing code across hierarchies
- Polymorphism – Overriding methods and behaviors
🏗️ Kotlin – Classes and Objects
class Person(val name: String) {
fun greet() = println("Hello, my name is $name")
}
val p = Person("Alice")
p.greet() // Output: Hello, my name is Alice
✅ Classes can have properties, functions, and initializers.
🚪 Kotlin – Constructors (Primary & Secondary)
Primary Constructor:
class Car(val brand: String, var speed: Int)
Secondary Constructor:
class Vehicle {
var type: String
constructor(t: String) {
this.type = t
}
}
📌 Use init
blocks for additional initialization:
class Book(val title: String) {
init {
println("Book '$title' created")
}
}
🧮 Kotlin – Class Functions / Methods
class Calculator {
fun add(a: Int, b: Int): Int = a + b
}
val calc = Calculator()
println(calc.add(5, 3)) // Output: 8
✅ Methods can be public by default or controlled with modifiers.
👪 Kotlin – Inheritance
open class Animal {
open fun sound() = println("Some sound")
}
class Dog : Animal() {
override fun sound() = println("Bark")
}
🧠 Use open
to allow a class or method to be inherited/overridden.
🧩 Kotlin – Abstract Classes
abstract class Shape {
abstract fun area(): Double
}
class Circle(val radius: Double) : Shape() {
override fun area() = Math.PI * radius * radius
}
📌 Abstract classes can contain both abstract and concrete methods.
🧷 Kotlin – Interfaces
interface Drawable {
fun draw()
}
class Rectangle : Drawable {
override fun draw() = println("Drawing Rectangle")
}
✅ Kotlin interfaces can include default implementations.
🔐 Kotlin – Visibility Modifiers
Modifier | Accessible From… |
---|---|
public | Everywhere (default) |
private | Only inside the file or class |
protected | Inside class and subclasses |
internal | Within the same module |
internal class Config {
private fun secret() = "Hidden"
}
📌 Summary – Recap & Next Steps
Kotlin enhances object-oriented programming by combining Java’s power with clean syntax, default immutability, and improved flexibility. Mastering OOP enables you to model real-world systems using reusable and modular structures.
🔍 Key Takeaways:
- Define clean class structures with constructors and properties.
- Use inheritance and polymorphism for reusable and extensible logic.
- Apply abstract classes and interfaces to enforce design contracts.
- Protect your logic using Kotlin’s visibility modifiers.
⚙️ Practical Use Cases:
- Creating model classes in Android MVVM
- Building reusable components in libraries
- Structuring domain logic with inheritance and interfaces
❓ Frequently Asked Questions
❓ What is the difference between open
and abstract
in Kotlin?
✅ open
allows optional overriding, while abstract
requires implementation in subclasses.
❓ Can Kotlin classes have multiple constructors?
✅ Yes. Kotlin supports both primary and secondary constructors.
❓ Are all classes in Kotlin final by default?
✅ Yes. Use open
to make them inheritable.
❓ Can a Kotlin class inherit multiple classes?
✅ No. Kotlin supports single inheritance but allows multiple interfaces.
❓ What’s the difference between interface and abstract class in Kotlin?
✅ Interfaces are contracts with no state. Abstract classes can hold state and partially implemented logic.
Share Now :