0
0
Kotlinprogramming~10 mins

Why extensions add without modifying in Kotlin - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why extensions add without modifying
Define original class
Write extension function
Call extension on instance
Extension runs using instance data
Original class code unchanged
Extensions add new functions to a class without changing its original code by defining separate functions that act like class methods.
Execution Sample
Kotlin
class Person(val name: String)

fun Person.greet() = "Hello, $name!"

val p = Person("Anna")
println(p.greet())
This code adds a greet function to Person without changing Person's code and calls it on an instance.
Execution Table
StepActionEvaluationResult
1Define class Person with property namePerson class createdPerson(name: String)
2Define extension function greet for PersonExtension greet addedfun Person.greet() = "Hello, $name!"
3Create instance p = Person("Anna")Instance p createdp.name = "Anna"
4Call p.greet()Extension function runs"Hello, Anna!"
5Print resultOutput to consoleHello, Anna!
💡 Program ends after printing greeting; original Person class code is unchanged.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
pundefinedPerson(name="Anna")Person(name="Anna")Person(name="Anna")
greet()undefineddefined as extensioncalled with preturns "Hello, Anna!"
Key Moments - 2 Insights
Why does the original Person class not change when we add greet()?
Because greet() is an extension function defined outside Person, it adds behavior without modifying Person's original code (see execution_table step 2).
How does greet() access the name property if it's outside the class?
Extension functions use the instance they are called on as 'this', so greet() can access p.name directly (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what does p.greet() return?
A"Hello, Anna!"
B"Hello, Person!"
CError: greet() not found
D"Hello, World!"
💡 Hint
Check the 'Result' column in execution_table row for step 4.
At which step is the extension function greet() defined?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table to find when greet() is added.
If we change the name property of p to "Bob" after creation, what will p.greet() return?
A"Hello, Bob!"
BError: name is immutable
C"Hello, Anna!"
D"Hello, Person!"
💡 Hint
Consider that name is a val property in Person class (see code in execution_sample).
Concept Snapshot
Extensions add new functions to existing classes without changing their code.
Syntax: fun ClassName.newFunction() { ... }
Called like normal methods on instances.
They access instance data via 'this'.
Original class remains unchanged.
Full Transcript
This example shows how Kotlin extension functions add new behavior to a class without modifying its original code. We define a Person class with a name property. Then we add an extension function greet() that returns a greeting using the name. When we create a Person instance p with name "Anna" and call p.greet(), it returns "Hello, Anna!". The original Person class code stays the same because greet() is defined outside it. Extension functions use the instance they are called on as 'this', so they can access properties like name. This lets us add useful functions without touching existing classes.