Edit the Kotlin example below and click Run to preview expected output notes.
Kotlin Tutorial
Learn Kotlin Tutorial with a small example you can edit and run.
fun main(){
// ...
}
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
println("Welcome to Kotlin") | Prints output. |
} | Kotlin line. |
- 1Kotlin is widely used for Android apps and JVM backends.
- 1Mixing up val vs var and mutating unexpectedly.
- 1Prefer val by default and keep functions small and clear.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin TutorialKotlin HOME
Learn Kotlin HOME with a small example you can edit and run.
fun main(){
// ...
}
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
println("Welcome to Kotlin") | Prints output. |
} | Kotlin line. |
- 1Kotlin is widely used for Android apps and JVM backends.
- 1Mixing up val vs var and mutating unexpectedly.
- 1Prefer val by default and keep functions small and clear.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin HOMEKotlin Intro
Learn Kotlin Intro with a small example you can edit and run.
fun main(){
// ...
}
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
println("Welcome to Kotlin") | Prints output. |
} | Kotlin line. |
- 1Kotlin is widely used for Android apps and JVM backends.
- 1Mixing up val vs var and mutating unexpectedly.
- 1Prefer val by default and keep functions small and clear.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin IntroKotlin Get Started
Learn Kotlin Get Started with a small example you can edit and run.
fun main(){
// ...
}
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
println("Welcome to Kotlin") | Prints output. |
} | Kotlin line. |
- 1Kotlin is widely used for Android apps and JVM backends.
- 1Mixing up val vs var and mutating unexpectedly.
- 1Prefer val by default and keep functions small and clear.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin Get StartedKotlin Syntax
Learn Kotlin Syntax with a small example you can edit and run.
fun main(){
// ...
}
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
val score = 82 | Declares a variable (val = immutable, var = mutable). |
val result = if (score >= 75) "Pass" else "Fail" | Declares a variable (val = immutable, var = mutable). |
println(result) | Prints output. |
} | Kotlin line. |
- 1Kotlin is widely used for Android apps and JVM backends.
- 1Mixing up val vs var and mutating unexpectedly.
- 1Prefer val by default and keep functions small and clear.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin SyntaxKotlin Output
Learn Kotlin Output with a small example you can edit and run.
println("Hello")
print("No newline")
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
print("Hi") | Prints output. |
print(" ") | Prints output. |
println("Kotlin") | Prints output. |
} | Kotlin line. |
- 1Kotlin is widely used for Android apps and JVM backends.
- 1Mixing up val vs var and mutating unexpectedly.
- 1Prefer val by default and keep functions small and clear.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin OutputKotlin Comments
Learn Kotlin Comments with a small example you can edit and run.
// single-line
/* multi-line */
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
/* Multi-line | Kotlin line. |
comment */ | Kotlin line. |
println("Comments don't change output") | Prints output. |
} | Kotlin line. |
- 1Kotlin is widely used for Android apps and JVM backends.
- 1Mixing up val vs var and mutating unexpectedly.
- 1Prefer val by default and keep functions small and clear.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin CommentsKotlin Variables
Learn Kotlin Variables with a small example you can edit and run.
val name: String = "Mana"
var age = 21
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
val name = "Mana" | Declares a variable (val = immutable, var = mutable). |
var level = 1 | Declares a variable (val = immutable, var = mutable). |
level += 1 | Kotlin line. |
println("$name level $level") | Prints output. |
} | Kotlin line. |
- 1Kotlin is widely used for Android apps and JVM backends.
- 1Mixing up val vs var and mutating unexpectedly.
- 1Prefer val by default and keep functions small and clear.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin VariablesKotlin Data Types
Learn Kotlin Data Types with a small example you can edit and run.
val n: Int = 7
val pi: Double = 3.14
val ok: Boolean = true
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
val a: Int = 10 | Declares a variable (val = immutable, var = mutable). |
val b: Double = 3.5 | Declares a variable (val = immutable, var = mutable). |
val ok: Boolean = true | Declares a variable (val = immutable, var = mutable). |
val ch: Char = 'A' | Declares a variable (val = immutable, var = mutable). |
println("$a, $b, $ok, $ch") | Prints output. |
} | Kotlin line. |
- 1Kotlin is widely used for Android apps and JVM backends.
- 1Mixing up val vs var and mutating unexpectedly.
- 1Prefer val by default and keep functions small and clear.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin Data TypesKotlin Operators
Learn Kotlin Operators with a small example you can edit and run.
val sum = a + b
val gt = a > b
val mod = a % b
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
val a = 7 | Declares a variable (val = immutable, var = mutable). |
val b = 3 | Declares a variable (val = immutable, var = mutable). |
println(a + b) | Prints output. |
} | Kotlin line. |
- 1Kotlin is widely used for Android apps and JVM backends.
- 1Mixing up val vs var and mutating unexpectedly.
- 1Prefer val by default and keep functions small and clear.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin OperatorsKotlin Strings
Learn Kotlin Strings with a small example you can edit and run.
val s = "Kotlin"
println("$s length=${s.length}")
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
val name = "Mana" | Declares a variable (val = immutable, var = mutable). |
val msg = "$name Coding" | Declares a variable (val = immutable, var = mutable). |
println(msg) | Prints output. |
println(msg.length) | Prints output. |
} | Kotlin line. |
- 1Strings are used for UI text, logs, and building messages.
- 1Concatenating in loops instead of using templates/builders.
- 1Use string templates and keep formatting consistent.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin StringsKotlin Booleans
Learn Kotlin Booleans with a small example you can edit and run.
val ok = (a >= 10) && (b < 5)
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
val a = 10 | Declares a variable (val = immutable, var = mutable). |
val b = 2 | Declares a variable (val = immutable, var = mutable). |
val ok = (a >= 10) && (b < 5) | Declares a variable (val = immutable, var = mutable). |
println(ok) | Prints output. |
} | Kotlin line. |
- 1Kotlin is widely used for Android apps and JVM backends.
- 1Mixing up val vs var and mutating unexpectedly.
- 1Prefer val by default and keep functions small and clear.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin BooleansKotlin If...Else
Learn Kotlin If...Else with a small example you can edit and run.
val label = if (score >= 75) "Pass" else "Fail"
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
val score = 75 | Declares a variable (val = immutable, var = mutable). |
if (score >= 90) println("A") | Prints output. |
else if (score >= 75) println("B") | Prints output. |
else println("C") | Prints output. |
} | Kotlin line. |
- 1Control flow powers validation, UI state, and business rules.
- 1Forgetting edge cases (empty lists, boundaries).
- 1Write readable conditions and test boundaries.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin If...ElseKotlin When
Learn Kotlin When with a small example you can edit and run.
val msg = when (day) {
1 -> "Mon"
else -> "Other"
}
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
val day = 3 | Declares a variable (val = immutable, var = mutable). |
val name = when (day) { | Declares a variable (val = immutable, var = mutable). |
1 -> "Mon" | Kotlin line. |
2 -> "Tue" | Kotlin line. |
3 -> "Wed" | Kotlin line. |
else -> "Other" | Kotlin line. |
} | Kotlin line. |
println(name) | Prints output. |
} | Kotlin line. |
- 1Control flow powers validation, UI state, and business rules.
- 1Forgetting edge cases (empty lists, boundaries).
- 1Write readable conditions and test boundaries.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin WhenKotlin While Loop
Learn Kotlin While Loop with a small example you can edit and run.
while (i <= 5) { i++ }
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
var n = 1 | Declares a variable (val = immutable, var = mutable). |
var sum = 0 | Declares a variable (val = immutable, var = mutable). |
while (n <= 5) { | Loop while condition is true. |
sum += n | Kotlin line. |
n++ | Kotlin line. |
} | Kotlin line. |
println(sum) | Prints output. |
} | Kotlin line. |
- 1Control flow powers validation, UI state, and business rules.
- 1Forgetting edge cases (empty lists, boundaries).
- 1Write readable conditions and test boundaries.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin While LoopKotlin Break/Continue
Learn Kotlin Break/Continue with a small example you can edit and run.
for (i in 1..10) {
if (i == 3) continue
if (i == 5) break
}
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
for (i in 1..6) { | Loop over a range/collection. |
if (i == 3) continue | Conditional branch. |
if (i == 5) break | Conditional branch. |
println(i) | Prints output. |
} | Kotlin line. |
} | Kotlin line. |
- 1Control flow powers validation, UI state, and business rules.
- 1Forgetting edge cases (empty lists, boundaries).
- 1Write readable conditions and test boundaries.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin Break/ContinueKotlin Arrays
Learn Kotlin Arrays with a small example you can edit and run.
val a = intArrayOf(1, 2, 3)
val b = arrayOf("A", "B")
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
val nums = intArrayOf(1, 2, 3, 4) | Declares a variable (val = immutable, var = mutable). |
var sum = 0 | Declares a variable (val = immutable, var = mutable). |
for (x in nums) sum += x | Loop over a range/collection. |
println(sum) | Prints output. |
} | Kotlin line. |
- 1Kotlin is widely used for Android apps and JVM backends.
- 1Mixing up val vs var and mutating unexpectedly.
- 1Prefer val by default and keep functions small and clear.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin ArraysKotlin For Loop
Learn Kotlin For Loop with a small example you can edit and run.
for (i in 1..3) println(i)
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
for (i in 1..3) { | Loop over a range/collection. |
print("i=$i ") | Prints output. |
} | Kotlin line. |
} | Kotlin line. |
- 1Control flow powers validation, UI state, and business rules.
- 1Forgetting edge cases (empty lists, boundaries).
- 1Write readable conditions and test boundaries.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin For LoopKotlin Ranges
Learn Kotlin Ranges with a small example you can edit and run.
for (i in 1..10 step 2) println(i)
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun main() { | Program entry point. |
for (i in 1..10 step 2) { | Loop over a range/collection. |
print("$i ") | Prints output. |
} | Kotlin line. |
} | Kotlin line. |
- 1Control flow powers validation, UI state, and business rules.
- 1Forgetting edge cases (empty lists, boundaries).
- 1Write readable conditions and test boundaries.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin RangesKotlin Functions
Learn Kotlin Functions with a small example you can edit and run.
fun add(a: Int, b: Int): Int = a + b
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
fun add(a: Int, b: Int): Int { | Declares a function. |
return a + b | Kotlin line. |
} | Kotlin line. |
fun main() { | Program entry point. |
println(add(4, 6)) | Prints output. |
} | Kotlin line. |
- 1Functions help you reuse logic and keep code DRY.
- 1Using unclear names and too many parameters.
- 1Prefer small, pure functions with clear inputs/outputs.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin FunctionsKotlin Classes
Learn Kotlin Classes with a small example you can edit and run.
class Person(val name: String) {
fun hi() = println("Hi $name")
}
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
class Person(val name: String) { | Declares a class. |
fun greet() { println("Hello, $name!") } | Declares a function. |
} | Kotlin line. |
fun main() { | Program entry point. |
val p = Person("Developer") | Declares a variable (val = immutable, var = mutable). |
p.greet() | Kotlin line. |
} | Kotlin line. |
- 1Classes model app domain objects and reusable components.
- 1Overusing inheritance instead of composition.
- 1Keep classes focused; use inheritance only when it fits.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin ClassesKotlin OOP
Learn Kotlin OOP with a small example you can edit and run.
class Person(val name: String) {
fun hi() = println("Hi $name")
}
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
class Person(val name: String) { | Declares a class. |
fun greet() { println("Hello, $name!") } | Declares a function. |
} | Kotlin line. |
fun main() { | Program entry point. |
val p = Person("Developer") | Declares a variable (val = immutable, var = mutable). |
p.greet() | Kotlin line. |
} | Kotlin line. |
- 1Classes model app domain objects and reusable components.
- 1Overusing inheritance instead of composition.
- 1Keep classes focused; use inheritance only when it fits.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin OOPKotlin Classes/Objects
Learn Kotlin Classes/Objects with a small example you can edit and run.
class Person(val name: String) {
fun hi() = println("Hi $name")
}
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
class User(val name: String) | Declares a class. |
fun main() { | Program entry point. |
val u = User("Mana") | Declares a variable (val = immutable, var = mutable). |
println(u.name) | Prints output. |
} | Kotlin line. |
- 1Classes model app domain objects and reusable components.
- 1Overusing inheritance instead of composition.
- 1Keep classes focused; use inheritance only when it fits.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin Classes/ObjectsKotlin Constructors
Learn Kotlin Constructors with a small example you can edit and run.
class User(val name: String) { /* primary ctor */ }
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
class User(val name: String, var level: Int) { | Declares a class. |
fun up() { level++ } | Declares a function. |
} | Kotlin line. |
fun main() { | Program entry point. |
val u = User("Mana", 1) | Declares a variable (val = immutable, var = mutable). |
u.up() | Kotlin line. |
println("${u.name} level ${u.level}") | Prints output. |
} | Kotlin line. |
- 1Classes model app domain objects and reusable components.
- 1Overusing inheritance instead of composition.
- 1Keep classes focused; use inheritance only when it fits.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin ConstructorsKotlin Class Functions
Learn Kotlin Class Functions with a small example you can edit and run.
fun add(a: Int, b: Int): Int = a + b
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
class Counter { | Declares a class. |
private var n = 0 | Kotlin line. |
fun inc() { n++ } | Declares a function. |
fun value(): Int = n | Declares a function. |
} | Kotlin line. |
fun main() { | Program entry point. |
val c = Counter() | Declares a variable (val = immutable, var = mutable). |
c.inc(); c.inc() | Kotlin line. |
println(c.value()) | Prints output. |
} | Kotlin line. |
- 1Functions help you reuse logic and keep code DRY.
- 1Using unclear names and too many parameters.
- 1Prefer small, pure functions with clear inputs/outputs.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin Class FunctionsKotlin Inheritance
Learn Kotlin Inheritance with a small example you can edit and run.
open class Animal { open fun speak(){} }
class Dog : Animal() { override fun speak(){} }
Edit the Kotlin code below and click Run. The output panel shows the extracted Expected Output notes (or simple prints).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
open class Animal { | Declares a class that can be inherited. |
open fun speak() { println("Animal") } | Prints output. |
} | Kotlin line. |
class Dog : Animal() { | Declares a class. |
override fun speak() { println("Woof") } | Prints output. |
} | Kotlin line. |
fun main() { | Program entry point. |
val a: Animal = Dog() | Declares a variable (val = immutable, var = mutable). |
a.speak() | Kotlin line. |
} | Kotlin line. |
- 1Classes model app domain objects and reusable components.
- 1Overusing inheritance instead of composition.
- 1Keep classes focused; use inheritance only when it fits.
- 1Edit the example and rerun to learn faster.
- 2Use readable Kotlin first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
Kotlin JVM Kotlin Inheritance