Inheritance ဆိုတာ class တစ်ခုက အခြား class တစ်ခုရဲ့ property/function တွေကို ဆက်ခံအသုံးပြုနိုင်တဲ့ OOP concept ပါ။ Kotlin မှာ class တွေကို default အနေနဲ့ inherit လုပ်ခွင့်မပေးပါ။ ဆက်ခံစေချင်တဲ့ class/function ကို open လို့ရေးပေးရပါတယ်။
kotlin
open class Animal {
open fun makeSound() {
println("Animal sound")
}
}
class Dog : Animal() {
override fun makeSound() {
println("Woof!")
}
}
fun main() {
val dog = Dog()
dog.makeSound()
}You should see
Woof!အနှစ်ချုပ်
Inheritance က common behavior ကို parent class ထဲမှာထားပြီး child class တွေကလိုအပ်သလို override လုပ်နိုင်စေပါတယ်။