All days
Day 04Module 2 · OOP in Kotlin· 3–5 hrs

OOP II — Abstract, Interfaces, Object, Companion Object

Interfaces vs abstract classes, Kotlin's `object` (singleton) and `companion object` (like Java statics). A common interview topic — get the mental model right.

0% done

Key concepts

  • abstract class & abstract fun
  • interface with default implementations
  • When to use interface vs abstract class
  • object declaration (singleton)
  • companion object (per-class statics)
  • sealed class basics

Lectures in this day

  1. 01

    Abstract Class and Method

    An `abstract` class is a blueprint that is not complete on its own. You can't build one directly — you must first create a child class that fills in the missing pieces. An `abstract` function has no body; each child MUST write one.

    Real life · The idea of 'Vehicle' is not something you can drive. You drive a real Car or Bike. But every Vehicle must be able to start.
    kotlin
    abstract class Vehicle(val name: String) {
        abstract fun start()                     0
        fun stop() = println("$name stopped")    1
    }
    
    class Car(name: String) : Vehicle(name) {
        override fun start() = println("$name: engine on")
    }
    
    Car("Toyota").start()   2

    Practice · 3 quick questions

    1. Q1.You can create an object of an abstract class directly?

    2. Q2.An abstract method…

    3. Q3.Abstract classes are good when…

    Pick one answer per question.
  2. 02

    Interface vs Abstract Class

    An interface is a promise: 'a class that says it has me MUST provide these functions'. A class can promise many interfaces. An abstract class is more like a partial family tree — a class can only have one parent, but that parent can already have some code inside.

    Real life · Interfaces are like skills on your CV — you can list many (can drive, can cook, can code). An abstract class is more like your family — you have only one.
    kotlin
    interface Drivable { fun drive() }
    interface Refuelable { fun refuel() = println("Refueling…") }  0
    
    abstract class Vehicle(val brand: String)                       1
    
    2
    class Sedan(brand: String) : Vehicle(brand), Drivable, Refuelable {
        override fun drive() = println("$brand cruising")
    }
    
    val s = Sedan("Honda")
    s.drive()
    s.refuel()

    Practice · 3 quick questions

    1. Q1.How many classes can Kotlin extend?

    2. Q2.How many interfaces can a class implement?

    3. Q3.Interfaces are best for…

    Pick one answer per question.
  3. 03

    Object and Companion Object

    `object Name { }` creates a class AND its one and only instance — a singleton (there is exactly one for the whole app). A `companion object` lives inside a class and holds things you use without making a new instance first, like constants or factory helpers.

    Real life · `object AppConfig` is the one settings sheet the whole company reads. `User.guest()` is a helper at the front desk that hands out a default guest account.
    kotlin
    object AppConfig {                       0
        const val API = "https:1
        var debug = false
    }
    
    class User private constructor(val name: String) {
        companion object {                    2
            fun guest() = User("Guest")
        }
    }
    
    println(AppConfig.API)          3
    println(User.guest().name)      4

    Practice · 3 quick questions

    1. Q1.`object` (singleton) means…

    2. Q2.`companion object` lives…

    3. Q3.Good use of a companion object?

    Pick one answer per question.
  4. 04

    Kotlin Extras

    An 'extension function' lets you add a new method to a type you didn't write — like teaching every String a new trick. A `sealed class` is a small closed family: you list all its children in one file, and Kotlin can then check that your `when` handled every case.

    Real life · Extension = sticking a new tool onto a Swiss Army knife. Sealed class = a switchboard with a fixed set of labeled buttons.
    kotlin
    0
    1
    fun String.shout(): String = this.uppercase() + "!"
    
    println("hello".shout())   2
    
    3
    4
    sealed class Result
    data class Ok(val value: Int) : Result()
    data class Err(val message: String) : Result()
    
    val r: Result = Ok(42)
    when (r) {
        is Ok  -> println("Got ${r.value}")
        is Err -> println("Failed: ${r.message}")
    }

    Practice · 3 quick questions

    1. Q1.A data class auto-generates…

    2. Q2.An extension function lets you…

    3. Q3.A sealed class is…

    Pick one answer per question.
  5. 05

    Module 2 Wrap-up / Summary

    You can now describe things with classes, share behavior with inheritance, promise skills with interfaces, share a single instance with `object`, and make quick helpers with companions.

    Real life · You've moved from single sentences to full paragraphs — code that other people can read, use, and extend.

    Practice · 3 quick questions

    1. Q1.Module 2 was about…

    2. Q2.Which is NOT in Module 2?

    3. Q3.After Module 2 you can…

    Pick one answer per question.

Real-life analogies

Interface vs abstract class

Interface = a driver's license (a capability). Abstract class = a car chassis (partial construction). You can hold many licenses but drive one chassis.

companion object

The 'front desk' of a class — accessible without an instance. Static factory methods and constants live here.

Code examples

Interface + abstract class

kotlin
interface Drivable {
    fun drive()                      0
    fun honk() = println("Beep!")    1
}

abstract class Engine(val cc: Int) {
    abstract fun start()
    fun status() = "Engine $cc cc"
}

class Sedan(cc: Int) : Engine(cc), Drivable {
    override fun start() = println("Vroom ${status()}")
    override fun drive() = println("Cruising...")
}

object & companion object

kotlin
object AppConfig {                 0
    const val API_BASE = "https:1
    var debug = false
}

class User private constructor(val id: Int, val name: String) {
    companion object {
        fun guest() = User(0, "Guest")   2
        const val MAX_NAME = 40
    }
}

println(AppConfig.API_BASE)
val g = User.guest()

Hands-on checklist

Milestone

Solid mental model of interface vs abstract vs companion object.

Common pitfalls

  • Trying to instantiate an abstract class — not allowed.
  • Confusing `object` (singleton) with `class` (blueprint).