0Pricing
Kotlin Academy · Lesson

Scoped Extensions and Companion Extensions

Limit extension visibility and add extensions to companion objects.

Why Scope Extensions?

By default, extensions are visible wherever imported. Scoping limits visibility to a class, file, or context — reducing namespace pollution and improving discoverability.

File-Private Extensions

Mark an extension function private at file level to scope it to that file only.

private fun String.shout() = uppercase() + "!"
fun main() {
    println("hello".shout()) // HELLO!
}

All lessons in this course

  1. Extension Functions: Syntax and Dispatch Rules
  2. Extension Properties and Computed Extensions
  3. Scoped Extensions and Companion Extensions
  4. Practical Extensions: Context, View & String Helpers
← Back to Kotlin Academy