Bird
Raised Fist0
Kotlinprogramming~5 mins

Abstract classes and methods in Kotlin

Choose your learning style10 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.

Practice

(1/5)
1. What is true about an abstract class in Kotlin?
easy
A. It cannot be instantiated directly and may contain abstract methods.
B. It can be instantiated like any other class.
C. It must not contain any methods with a body.
D. It is the same as an interface.

Solution

  1. Step 1: Understand abstract class instantiation rules

    Abstract classes cannot create objects directly because they are incomplete by design.
  2. Step 2: Check method types in abstract classes

    Abstract classes can have both abstract methods (without body) and concrete methods (with body).
  3. Final Answer:

    It cannot be instantiated directly and may contain abstract methods. -> Option A
  4. Quick Check:

    Abstract class = no direct objects + can have abstract methods [OK]
Hint: Abstract classes can't create objects directly [OK]
Common Mistakes:
  • Thinking abstract classes can be instantiated
  • Believing abstract classes can't have method bodies
  • Confusing abstract classes with interfaces
2. Which of the following is the correct way to declare an abstract method in a Kotlin abstract class?
easy
A. abstract fun calculate(): Int
B. fun abstract calculate(): Int
C. fun calculate() abstract: Int
D. abstract calculate fun(): Int

Solution

  1. Step 1: Recall Kotlin syntax for abstract methods

    In Kotlin, abstract methods are declared with the abstract keyword before fun, followed by the method signature without a body.
  2. Step 2: Check each option's syntax

    Only abstract fun calculate(): Int matches the correct syntax: abstract fun calculate(): Int. Others have incorrect keyword order or punctuation.
  3. Final Answer:

    abstract fun calculate(): Int -> Option A
  4. Quick Check:

    abstract fun method() = correct syntax [OK]
Hint: abstract keyword comes before fun in method declaration [OK]
Common Mistakes:
  • Placing abstract after fun
  • Adding a method body to abstract methods
  • Incorrect keyword order
3. What will be the output of this Kotlin code?
abstract class Animal {
    abstract fun sound(): String
}

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

fun main() {
    val dog = Dog()
    println(dog.sound())
}
medium
A. Animal
B. sound
C. Bark
D. Compilation error

Solution

  1. Step 1: Understand class inheritance and method override

    The abstract class Animal has an abstract method sound(). The subclass Dog overrides it to return "Bark".
  2. Step 2: Analyze main function output

    Creating a Dog object and calling sound() prints "Bark".
  3. Final Answer:

    Bark -> Option C
  4. Quick Check:

    Overridden method returns "Bark" [OK]
Hint: Overridden abstract method returns subclass value [OK]
Common Mistakes:
  • Expecting abstract class method to run
  • Confusing method name with output
  • Thinking abstract class can be instantiated
4. Identify the error in this Kotlin code:
abstract class Vehicle {
    abstract fun drive()
}

class Car : Vehicle() {
    fun drive() {
        println("Driving")
    }
}
medium
A. drive method in Vehicle should have a body
B. Vehicle class cannot be abstract
C. Car class cannot inherit from Vehicle
D. Missing override keyword in Car's drive method

Solution

  1. Step 1: Check method overriding rules in Kotlin

    When a subclass implements an abstract method, it must use the override keyword.
  2. Step 2: Identify missing override keyword

    The Car class defines drive() without override, causing a compilation error.
  3. Final Answer:

    Missing override keyword in Car's drive method -> Option D
  4. Quick Check:

    Override keyword required for abstract method implementation [OK]
Hint: Use override keyword when implementing abstract methods [OK]
Common Mistakes:
  • Omitting override keyword
  • Adding body to abstract method in base class
  • Misunderstanding inheritance rules
5. Given this abstract class and subclass, what will be the output?
abstract class Shape {
    abstract fun area(): Double
    fun description() = "Shape area is ${area()}"
}

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

fun main() {
    val c = Circle(2.0)
    println(c.description())
}
hard
A. Shape area is 6.28
B. Shape area is 12.56
C. Shape area is 4.0
D. Compilation error due to abstract method

Solution

  1. Step 1: Calculate area in Circle subclass

    Area = π * radius² = 3.14 * 2.0 * 2.0 = 12.56
  2. Step 2: Call description method from Shape

    Description returns string with area value: "Shape area is 12.56".
  3. Final Answer:

    Shape area is 12.56 -> Option B
  4. Quick Check:

    Abstract method overridden, description uses area() [OK]
Hint: description calls overridden area() method [OK]
Common Mistakes:
  • Forgetting to override abstract method
  • Miscomputing area formula
  • Expecting abstract class method to run alone
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard