Skip to content
Learn Netverks

Lesson

Step 21/36 58% through track

extension-functions

Extension functions

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Extension functions: the syntax, patterns, and safety habits you need before advancing in Kotlin.

Teams still ship Extension functions in Kotlin codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Extension functions in contexts like: Android apps, Spring services, and shared KMP modules.

Write Kotlin in main.kt with fun main(), click Run on server—the dev runner kotlinc compiles to a JVM jar and java runs it; use println for output (requires JDK + kotlinc; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Extension functions add methods to existing types without inheritance or wrapper classes—resolved statically at compile time. They power Kotlin stdlib conveniences like String.trimIndent().

Declaring extensions

fun String.wordCount(): Int =
    trim().split(Regex("\s+")).size

Extensions do not modify the original class bytecode; they are syntactic sugar calling static helpers with the receiver as the first argument.

Guidelines

  • Prefer extensions for domain-specific helpers on types you do not own
  • Avoid hiding member functions—extensions never override members
  • Keep extensions close to their domain package

Important interview questions and answers

  1. Q: Do extensions add fields?
    A: No—only methods; state still lives in the receiver or wrappers.
  2. Q: Static resolution?
    A: The extension called depends on the declared static type, not runtime subtype—unlike virtual methods.

Self-check

  1. What keyword introduces the receiver type?
  2. Can extensions override class methods?

Pitfall: Extensions do not add members to the class—they are static helpers resolved at compile time.

Interview prep

Do extensions modify classes?

No—compile-time syntactic sugar on receiver type.

When to use?

Adapt third-party APIs without subclassing.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Extension vs inherit?
  • Scope of extension?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump