Thuta Learning
ရှာဖွေရန်
IntermediateProgrammingbeginner

Inheritance

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

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 လုပ်နိုင်စေပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Parent class/function ကို open မရေးထားရင် child class က inherit/override လုပ်လို့မရပါ။
Inheritance | Thuta Learning