0
0
Kotlinprogramming~5 mins

Abstract classes and methods in Kotlin

Choose your learning style9 modes available
Introduction

Abstract classes let you create a basic plan for other classes. Abstract methods are like empty boxes that child classes must fill in.

When you want to define a common structure for different types of objects but leave some details to be decided later.
When you have a general idea but want to force specific classes to provide their own version of some functions.
When you want to share code between classes but also have some methods that must be implemented by each class.
When designing a game where different characters share some actions but act differently in others.
When creating a shape drawing program where each shape must calculate its area differently.
Syntax
Kotlin
abstract class ClassName {
    abstract fun methodName()
    fun normalMethod() {
        // code
    }
}

An abstract class cannot be created directly with ClassName().

Abstract methods have no body and must be implemented in subclasses.

Examples
Animal is abstract with an abstract method. Dog must provide its own makeSound method.
Kotlin
abstract class Animal {
    abstract fun makeSound()
}

class Dog : Animal() {
    override fun makeSound() {
        println("Woof")
    }
}
Vehicle has an abstract method and a normal method. Car implements the abstract one and inherits the normal one.
Kotlin
abstract class Vehicle {
    abstract fun startEngine()
    fun stopEngine() {
        println("Engine stopped")
    }
}

class Car : Vehicle() {
    override fun startEngine() {
        println("Car engine started")
    }
}
You cannot create an object of an abstract class directly.
Kotlin
abstract class Empty {
    abstract fun doSomething()
}

// Error: Cannot create instance of abstract class
// val test = Empty()
Even if there is only one abstract method, subclasses must implement it.
Kotlin
abstract class SingleMethod {
    abstract fun action()
}

class SingleImpl : SingleMethod() {
    override fun action() {
        println("Action done")
    }
}
Sample Program

This program shows an abstract class Shape with an abstract method area. Circle and Square provide their own area calculations. The describe method is shared.

Kotlin
abstract class Shape {
    abstract fun area(): Double
    fun describe() {
        println("I am a shape")
    }
}

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

class Square(private val side: Double) : Shape() {
    override fun area(): Double {
        return side * side
    }
}

fun main() {
    val circle = Circle(2.0)
    val square = Square(3.0)

    circle.describe()
    println("Circle area: ${circle.area()}")

    square.describe()
    println("Square area: ${square.area()}")
}
OutputSuccess
Important Notes

Time complexity depends on the implementation of abstract methods, but calling an abstract method itself is simple and fast.

Space complexity is normal for classes; abstract classes do not add extra memory overhead.

A common mistake is trying to create an object of an abstract class directly, which is not allowed.

Use abstract classes when you want to share code and force subclasses to implement certain methods. Use interfaces if you only want to define methods without any code.

Summary

Abstract classes provide a base with some methods to implement later.

Abstract methods have no body and must be implemented by subclasses.

You cannot create objects of abstract classes directly.