Thuta Learning
Kotlin
IntermediateProgrammingအခြေခံ

Inheritance

Kotlinသင်ခန်းစာ 1630 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Kotlin class နဲ့ member တွေက default final ဖြစ်ရတဲ့ အကြောင်းနဲ့ open က ဘာကို ပြောင်းလဲ ရှင်းပြနိုင်ဖို့
  • override နဲ့ function တွေ အစားထိုးပြီး super နဲ့ parent version ကို ခေါ်ကာ parent constructor ကို argument ပေးနိုင်ဖို့
  • abstract နဲ့ open member တွေပါတဲ့ abstract class ကို ဒီဇိုင်းလုပ်ပြီး polymorphic ပုံစံ သုံးနိုင်ဖို့
  • Constructor ဒါမှမဟုတ် init block ထဲက open function ကို ခေါ်တဲ့ trap ကို ရှောင်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Inheritance ဆိုတာ class တစ်ခုက တခြား class တစ်ခုရဲ့ property နဲ့ function တွေကို ပြန်မရေးဘဲ အဲဒီကနေ စတင်သုံးတာပါ။ မိသားစု ဟင်းချက်နည်းကို စဉ်းစားကြည့်ပါ — ကလေးက မိဘရဲ့ ချက်နည်းအတိုင်း လုပ်နိုင်တယ်၊ ဆားနည်းနည်း ထပ်ထည့်နိုင်တယ်၊ ဒါမှမဟုတ် အဆင့်တစ်ခုကို လုံးဝ အစားထိုးနိုင်ပါတယ်။ Code မှာ Dog က Animal ကနေ inherit လုပ်လို့ Dog တစ်ကောင်က Animal လုပ်နိုင်သမျှ လုပ်နိုင်ပြီး တစ်ချို့ကို လုပ်ပုံ ပြောင်းလို့ရပါတယ်။

Kotlin က ဒီမှာ ဂရုတစိုက် ရှိပါတယ်။ Class ရေးသူက open လို့ မရေးရင် အဲဒီ class ကို extend လုပ်လို့မရပါဘူး၊ member ကိုလည်း open မဟုတ်ရင် override မလုပ်နိုင်ပါဘူး။ တခြားသူ့ class ကို inherit လုပ်တာက သူ ပြောင်းရင်တောင် ဆက်အလုပ်လုပ်ပေးမယ်ဆိုတဲ့ ကတိတစ်ခုမို့ ရေးသူက ကိုယ်တိုင် ခွင့်ပြုရပါတယ်။

Problem → Why → Solution

ပြဿနာ: string tag နဲ့ when တွေ အနှံ့ပြန့်နေတာ

အိမ်မွေးတိရစ္ဆာန် app တစ်ခုက တိရစ္ဆာန်အမျိုးအစားကို string အဖြစ် သိမ်းပြီး behaviour တိုင်းကို အဲဒီ string ပေါ်က when နဲ့ ရေးထားတယ်ဆိုပါစို့။ Hamster အသစ်ထည့်ချင်ရင် code base ထဲက when တိုင်းကို ရှာပြီး branch ထပ်ထည့်ရပါမယ်။ တစ်ခု လွတ်သွားရင် hamster က else ထဲကို တိတ်တိတ်လေး ကျသွားပါတယ်။ တိရစ္ဆာန်တစ်ကောင်ရဲ့ behaviour က တိရစ္ဆာန်နဲ့အတူ မရှိဘဲ function တွေကြား ပြန့်ကျဲနေတာပါ။

string-tags.kt
fun sound(kind: String): String = when (kind) {
    "dog" -> "Woof"
    "cat" -> "Meow"
    else -> "..."
}

fun describe(kind: String): String = when (kind) {
    "dog" -> "a loyal dog"
    "cat" -> "an independent cat"
    else -> "an animal"
}

fun main() {
    println(sound("hamster"))   // "..." because nobody remembered the new kind
}

String tag က ဘာကြောင့် မကောင်းတာလဲ

String tag က စာလုံးပေါင်းမှားတာနဲ့ branch မေ့တာတွေကို ဖိတ်ခေါ်ပါတယ်၊ ဘယ်အမျိုးအစားတွေ ရှိတယ်ဆိုတာ compiler က မသိလို့ပါ။ Inheritance က ရွေးချယ်မှုကို object ကိုယ်တိုင်ထဲ ရွှေ့ပေးပါတယ် — class တစ်ခုစီက ကိုယ်ပိုင် sound() ကို ကိုင်ထားပြီး animal.makeSound() ခေါ်ရင် runtime မှာ မှန်တဲ့ version ကို ရွေးပါတယ် (polymorphism)။ Hamster ထပ်ထည့်ဖို့ class တစ်ခု ထပ်ရေးရုံပါပဲ၊ makeSound() ကို ခေါ်တဲ့ code ကတော့ မပြောင်းရပါဘူး။

Core concept: open, override နဲ့ super

Inherit လုပ်ဖို့ keyword နှစ်ခု လိုပါတယ်။ Parent က open class Animal လို့ ရေးပြီး child တွေ အစားထိုးလို့ရမယ့် member တိုင်းကိုလည်း open fun makeSound() လို့ open လုပ်ပါတယ်။ Child က colon နောက်မှာ parent ကို နာမည်ပေးပြီး parent constructor ကို ခေါ်ပါတယ်: class Dog : Animal()။ open မပါရင် compile error ဖြစ်ပါတယ် — class အတွက် "this type is final, so it cannot be extended"၊ function အတွက် "'makeSound' in 'Animal' is final and cannot be overridden" ပါ။

Override လုပ်တာက နှစ်ဖက်စလုံးမှာ ပွင့်ပွင့်လင်းလင်း ပါ။ Child က override လို့ ရေးရမယ်၊ မရေးရင် "'makeSound' hides member of supertype 'Animal' and needs an 'override' modifier" error ပါ။ Override လုပ်ထားတဲ့ member က final override လို့ မရေးမချင်း နောက်ထပ် subclass တွေအတွက် open ဆက်ဖြစ်နေပါတယ်။ ထဲမှာ super.makeSound() လို့ ခေါ်ရင် parent ရဲ့ version ကို run ပါတယ် — ဒါက အစားထိုးတာမဟုတ်ဘဲ behaviour ကို ထပ်တိုးတဲ့ နည်းပါ။

Parent မှာ constructor parameter ရှိရင် child က ပို့ပေးရပါမယ်: class Dog(name: String) : Animal(name)။ Abstract class က တစ်ဆင့် ထပ်သွားပါတယ် — body မပါတဲ့ abstract member တွေ ကြေညာနိုင်ပြီး ကိုယ်တိုင် instance မဖန်တီးနိုင်ပါဘူး ("cannot create an instance of an abstract class")၊ concrete child တိုင်းကို အဲဒီ member တွေ ဖြည့်ခိုင်းပါတယ်။ Abstract member တွေက သဘာဝအတိုင်း open ဖြစ်လို့ abstract ဘေးမှာ open ထပ်မရေးရပါဘူး။

အကျိုးကျေးဇူးက polymorphism ပါ။ Animal type variable တစ်ခုက Dog ဒါမှမဟုတ် Cat ကို ကိုင်နိုင်ပြီး animal.makeSound() က တကယ့် object ရဲ့ version ကို runtime မှာ ရွေးပြီး run ပါတယ်။ တကယ့် type ကို is နဲ့ စစ်လို့ရပြီး branch ထဲမှာ Kotlin က smart cast လုပ်ပေးပါတယ်။ Hierarchy ကို တိမ်တိမ်လေး ထားပါ — အဆင့်များလေ ဘယ် version run လဲ မြင်ရခက်လေပါပဲ၊ inheritance က "is-a" ဆက်ဆံရေး စစ်စစ်အတွက်သာ အကောင်းဆုံးပါ။

Modifierအဓိပ္ပာယ်Child ရဲ့ တာဝန်
openInherit ဒါမှမဟုတ် override လုပ်ခွင့်ရှိOverride လုပ်လို့ရတယ်
abstractBody မပါ၊ class ကို instance မလုပ်နိုင်မဖြစ်မနေ override လုပ်ရမယ်
overrideParent member ကို အစားထိုးတယ်final မလုပ်မချင်း open ဆက်ဖြစ်
final (default)Extend/override လုပ်လို့မရဘာမှ မလုပ်နိုင်
မှတ်ထားရမယ့် ပုံစံ: override အဆင့်ဆင့်
  1. Variable type က parent (Animal)
  2. တကယ့် object က child (Dog)
  3. Call က child ရဲ့ override ကို run
  4. super.x() က parent version ကို ခေါ်နိုင်

ဘယ် function run မလဲဆိုတာကို variable ရဲ့ type မဟုတ်ဘဲ တကယ့် object ရဲ့ class က ဆုံးဖြတ်ပါတယ်။

အထွေထွေ ပုံစံ

kotlin
open class Parent(val param: Type) {
    open fun action() { /* default behaviour */ }
    fun fixed() { /* cannot be overridden */ }
}

class Child(param: Type) : Parent(param) {
    override fun action() {
        super.action()     // optional: run the parent version too
        // extra behaviour
    }
}

Parent မှာ class နဲ့ override ခွင့်ပေးမယ့် function ကို open လုပ်ပါတယ်။ open မဟုတ်တဲ့ fixed() ကိုတော့ child က အစားထိုးလို့မရပါဘူး။ Child က colon နောက်မှာ Parent(param) လို့ parent constructor ကို ခေါ်ပြီး override လုပ်တဲ့ function ထဲမှာ super.action() ကို လိုသလို ခေါ်ပါတယ်။

Example: Dog က Animal ရဲ့ အသံကို အစားထိုးတယ်

Animal က default အသံတစ်ခု ပေးပြီး Dog က အဲဒါကို အစားထိုးပါတယ်။ Dog မှာ super.makeSound() မခေါ်ထားလို့ Animal ရဲ့ "Animal sound" ကို ဘယ်တော့မှ print မထုတ်ပါဘူး၊ main က Woof! တစ်ကြောင်းတည်း မြင်ရမှာပါ။ ပြီးရင် Dog ထဲမှာ super.makeSound() တစ်ကြောင်း ထပ်ထည့်ကြည့်ပြီး ရလဒ် ဘယ်လိုပြောင်းလဲ ခန့်မှန်းကြည့်ပါ။

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()
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Open the parent classopen class Animal {

    open ကြောင့် တခြား class တွေက Animal ကို extend လုပ်လို့ရပါတယ်။ ဒါမပါရင် class က final ဖြစ်ပြီး Dog : Animal() က "this type is final, so it cannot be extended" နဲ့ fail ဖြစ်ပါတယ်။

  2. Open the memberopen fun makeSound() {

    Class ကို open လုပ်ရုံနဲ့ မလုံလောက်ပါဘူး၊ child တွေ အစားထိုးခွင့်ရတဲ့ function ကိုလည်း တစ်ခုချင်း open လုပ်ရပါတယ်။ ဒါကြောင့် Kotlin မှာ parent က ဘယ်အပိုင်းတွေ ပြောင်းလို့ရမလဲ ရွေးချယ်နိုင်ပါတယ်။

  3. Name the parentclass Dog : Animal() {

    Colon နောက်က Animal() က parent ကို နာမည်ပေးပြီး parent constructor ကို ခေါ်ပါတယ်။ Animal မှာ parameter မရှိလို့ ကွင်းက အလွတ်ပါ၊ parameter ရှိရင် ဒီမှာ argument တွေ ပို့ပေးရပါမယ်။

  4. Overrideoverride fun makeSound() {

    override က ဒီ function က parent ရဲ့ makeSound ကို အစားထိုးတာလို့ ကြေညာပါတယ်။ ဒါကြောင့် နာမည်၊ parameter နဲ့ return type တွေ parent နဲ့ ကိုက်ရပါမယ်။ override မပါရင် error ပါ။

  5. Replace the behaviourprintln("Woof!")

    Dog ရဲ့ body က parent ရဲ့ body ကို အစားထိုးတာမို့ "Animal sound" မထွက်ပါဘူး။ Parent အလုပ်ကိုလည်း ဆက်လုပ်စေချင်ရင် ဒီ line မတိုင်ခင် super.makeSound() ခေါ်ရပါမယ်။

  6. Call itdog.makeSound()

    Runtime မှာ dog ရဲ့ တကယ့် class က Dog ဖြစ်လို့ Dog ရဲ့ version ကို run ပါတယ်။ ဒီ variable ကို val dog: Animal = Dog() လို့ ကြေညာထားလည်း Woof! ထွက်တာ အတူတူပါပဲ။

You should see
Woof!
dog.makeSound() ခေါ်တဲ့အခါ
  1. dog.makeSound() ကို ခေါ်
  2. တကယ့် class ကို ရှာ — Dog
  3. Dog က makeSound() ကို override လုပ်ထားလား

Override လုပ်ထား

Dog's version runs and prints Woof!

မလုပ်ထား

the inherited Animal version runs

Override, super နဲ့ polymorphism

ဒီ snippet မှာ Cat က super.makeSound() ကို ခေါ်ပြီး parent ရဲ့ အလုပ်ကို ထပ်ပေါင်းပါတယ်၊ Dog ကတော့ လုံးဝ အစားထိုးပါတယ်။ List<Animal> ထဲမှာ Dog, Cat နဲ့ Animal ကိုယ်တိုင်ကို ရောထည့်ထားပေမယ့် p.makeSound() ကို တစ်ခုချင်း ခေါ်တဲ့အခါ ကိုယ့်ရဲ့ version ကိုယ်စီ run ပါတယ်။ is က တကယ့် class ကို စစ်ပြီး if (a is Dog) ထဲမှာ a ကို Dog လို့ smart cast လုပ်ပေးလို့ cast မရေးရပါဘူး။

polymorphism.kt
open class Animal {
    open fun makeSound() {
        println("Animal sound")
    }
}

class Dog : Animal() {
    override fun makeSound() {
        println("Woof!")
    }
}

class Cat : Animal() {
    override fun makeSound() {
        super.makeSound()
        println("Meow")
    }
}

fun main() {
    val dog = Dog()
    dog.makeSound()
    val pets: List<Animal> = listOf(Dog(), Cat(), Animal())
    for (p in pets) p.makeSound()
    val a: Animal = Dog()
    println(a is Dog)
    println(a is Cat)
    if (a is Dog) println("smart cast ok")
}

Abstract class နဲ့ constructor argument ပို့ခြင်း

Employee က abstract ဖြစ်ပြီး bonus() ကို body မပါဘဲ ကြေညာထားလို့ Engineer နဲ့ Manager တိုင်းက bonus() ကို ဖြည့်ရပါမယ်။ pay() က open ဖြစ်လို့ Manager က super.pay() ကို ခေါ်ပြီး 1000 ထပ်ပေါင်းပါတယ်။ describe() ကိုတော့ open မလုပ်ထားလို့ child တွေက မပြောင်းနိုင်ပါဘူး — ဒီ function က အမြဲတမ်း title(), name နဲ့ pay() ကို ပေါင်းထားမယ်ဆိုတဲ့ ကတိပါ။ Child တစ်ခုစီက Employee(name, baseSalary) လို့ parent constructor ကို argument ပို့ပေးတာကိုလည်း သတိထားပါ။ protected ဆိုတာ class နဲ့ သူ့ subclass တွေကပဲ မြင်ရတဲ့ visibility ပါ။

employees.kt
abstract class Employee(val name: String, protected val baseSalary: Int) {
    abstract fun bonus(): Int
    open fun pay(): Int = baseSalary + bonus()
    open fun title(): String = "Employee"
    fun describe(): String = "${title()} $name earns ${pay()}"
}

class Engineer(name: String, baseSalary: Int) : Employee(name, baseSalary) {
    override fun bonus(): Int = 500
    override fun title(): String = "Engineer"
}

class Manager(
    name: String,
    baseSalary: Int,
    private val reports: Int,
) : Employee(name, baseSalary) {
    override fun bonus(): Int = reports * 200
    override fun pay(): Int = super.pay() + 1000
    override fun title(): String = "Manager"
}

fun main() {
    val staff: List<Employee> = listOf(Engineer("Aye", 3000), Manager("Kyaw", 4000, 3))
    for (e in staff) println(e.describe())
    println(staff.sumOf { it.pay() })
}

Trap: constructor ထဲက open function ကို ခေါ်တာ

Object တစ်ခု ဆောက်တဲ့အခါ parent ရဲ့ init block က child ရဲ့ property initializer တွေထက် အရင် run ပါတယ်။ Parent init ထဲက open function ကို ခေါ်ရင် child ရဲ့ override ကို run ပေမယ့် အဲဒီအချိန်မှာ child ရဲ့ property တွေ မ initialise လုပ်ရသေးပါဘူး။ အောက်မှာ Child.label() က name ကို ဖတ်ပေမယ့် Base ရဲ့ init က ခေါ်တဲ့အချိန်မှာ name က null ပါ — String လို့ ကြေညာထားတာတောင် ဒီလိုဖြစ်ပါတယ်။ Compile လည်း ဖြစ်၊ crash လည်း မဖြစ်ဘဲ မှားတာကို တိတ်တိတ်လေး ပြန့်ပွားစေလို့ ပိုအန္တရာယ်ရှိပါတယ်။

open-in-init.kt
open class Base {
    init {
        println("Base init sees: " + label())
    }

    open fun label(): String = "base"
}

class Child : Base() {
    private val name = "child"

    override fun label(): String = "I am " + name
}

fun main() {
    Child()                        // prints "I am null": name is not set yet
    println(Child().label())       // by now the object is complete
}

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Framework တွေက inheritance ကို အများကြီး သုံးပါတယ်။ Android မှာ class MainActivity : AppCompatActivity() လို့ ရေးပြီး onCreate ကို override လုပ်ကာ super.onCreate(...) ကို မဖြစ်မနေ ခေါ်ရပါတယ်။ ကိုယ်ပိုင် exception တွေကလည်း class ValidationException(message: String) : Exception(message) ပုံစံပါ။ UI toolkit တွေမှာ screen အားလုံးအတွက် အခြေခံ ဘုံ behaviour တွေကို abstract base class တစ်ခုထဲမှာ ထားပြီး screen တစ်ခုစီက လိုအပ်တဲ့ ပိုင်းကိုပဲ override လုပ်ပါတယ်။

Final-by-default ကြောင့် Spring လို framework တွေက class တွေကို proxy လုပ်ဖို့ ဆိုရင် open လုပ်ပေးရပါတယ်။ Kotlin မှာ kotlin-spring ဆိုတဲ့ compiler plugin က Spring annotation ပါတဲ့ class တွေကို အလိုအလျောက် open လုပ်ပေးပါတယ်။ ဒါပေမယ့် ကိုယ်ပိုင် business code မှာတော့ inheritance ကို "is-a" ဆက်ဆံရေး စစ်စစ်မှာပဲ သုံးပြီး behaviour ပြန်သုံးချင်ရုံနဲ့ဆိုရင် composition (object တစ်ခုကို property အဖြစ်ကိုင်တာ) နဲ့ interface တွေကို ဦးစားပေးပါ၊ ဒါက hierarchy ပြောင်းလဲမှုတွေကြောင့် subclass တွေ ပျက်တာကို လျှော့ပေးပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Parent class ကို open မလုပ်တာ

class Animal နဲ့ class Dog : Animal() ဆိုရင် "this type is final, so it cannot be extended" error ပေါ်ပါတယ်။ Function ကိုလည်း open မလုပ်ဘဲ override လုပ်ရင် "'makeSound' in 'Animal' is final and cannot be overridden" ပါ။ ကိုယ်ပိုင် parent ဆိုရင် class နဲ့ function ကို open လုပ်ပါ၊ တခြားသူ့ class ဆိုရင်တော့ composition ကို စဉ်းစားပါ။

2. override မေ့တာ

Parent မှာ open fun makeSound() ရှိပြီး child မှာ fun makeSound() လို့ပဲ ရေးရင် "'makeSound' hides member of supertype 'Animal' and needs an 'override' modifier" error ပါ။ Kotlin က မရည်ရွယ်ဘဲ နာမည်တူတာကို ကာကွယ်ဖို့ ဒီလို တင်းကျပ်ထားတာပါ။

3. Parent constructor ကို argument မပေးတာ

open class Animal(val name: String) ရှိပြီး class Dog : Animal() လို့ ရေးရင် "no value passed for parameter 'name'" error ပါ။ class Dog(name: String) : Animal(name) လို့ ရေးပြီး တန်ဖိုးကို ပို့ပေးပါ။

4. Constructor ထဲက open function ကို ခေါ်တာ

Parent ရဲ့ init ထဲက open function ကို ခေါ်ရင် child ရဲ့ override က child property မ initialise မလုပ်ရသေးခင် run ပြီး null ဒါမှမဟုတ် 0 ကို ဖတ်မိပါတယ်။ အပေါ်က deeper section မှာ ပြထားပါတယ်။ Constructor ထဲမှာ open member မခေါ်ပါနဲ့။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Behaviour ပြန်သုံးချင်ရုံနဲ့ inherit မလုပ်ပါနဲ့၊ "is-a" ဆက်ဆံရေး စစ်စစ်ဖြစ်မှ inherit လုပ်ပြီး ကျန်တာကို composition နဲ့ interface နဲ့ ဖြေရှင်းပါ။
  • Open လုပ်တဲ့ အပိုင်းကို နည်းနည်းပဲ ထားပြီး child တွေ ဘယ်လို override လုပ်ရမလဲ documentation ထဲ ရေးပါ၊ ဒီဇိုင်း ပြီးရင် final override နဲ့ ပိတ်ပါ။
  • Hierarchy ကို နှစ်ဆင့် သုံးဆင့်ထက် မနက်စေပါနဲ့၊ Constructor နဲ့ init ထဲမှာ open member ကို မခေါ်ပါနဲ့။

Mini challenge

Mini challenge: Shape hierarchy

Abstract Shape class တစ်ခုနဲ့ concrete shape သုံးမျိုး ရေးပါ။ ဧရိယာအလိုက် စီပြီး ဖော်ပြချက်တွေ print ထုတ်ပါ။

  • abstract class Shape(val name: String) မှာ abstract fun area(): Double နဲ့ name နဲ့ area() ကို သုံးတဲ့ open describe() ပါရမယ်
  • Rectangle(w, h) နဲ့ Triangle(base, height) က area() ကို override လုပ်ရမယ်
  • Square(side) က area() ကို override လုပ်ပြီး super.describe() ကို ခေါ်ကာ describe() ကို ချဲ့ရမယ်
  • main က shape တွေကို ဧရိယာအလိုက် စီပြီး ဖော်ပြချက် တစ်ခုချင်း print ထုတ်ပြီး အကြီးဆုံး shape ရဲ့ နာမည်ကို print ထုတ်ရမယ်
shapes.kt
abstract class Shape(val name: String) {
    // TODO: abstract fun area(): Double
    // TODO: open fun describe(): String   ("<name> with area <area>")
}

// TODO: class Rectangle(w: Double, h: Double) : Shape("Rectangle")
// TODO: class Triangle(base: Double, height: Double) : Shape("Triangle")
// TODO: class Square(side: Double) : Shape("Square") that extends describe() with super

fun main() {
    // TODO: listOf(Rectangle(4.0, 3.0), Triangle(5.0, 5.0), Square(2.0))
    // TODO: print every description, smallest area first
    // TODO: print the name of the shape with the largest area
}

ရလဒ်: Program က ဒီ line လေးကြောင်းကို ထုတ်ရပါမယ်: Square with area 4.0 (all sides 2.0) Rectangle with area 12.0 Triangle with area 12.5 Triangle

အရိပ်အမြွက် ကြည့်ရန်

shapes.sortedBy { it.area() } နဲ့ စီပါ၊ အကြီးဆုံးကို shapes.maxByOrNull { it.area() }?.name နဲ့ ရပါတယ်။ Square ရဲ့ describe() ကို super.describe() + " (all sides " + side + ")" လို့ ရေးပါ။ Double တွေက 4.0 ပုံစံနဲ့ print ထွက်ပါတယ်။

အဖြေ ကြည့်ရန်
shapes.kt
abstract class Shape(val name: String) {
    abstract fun area(): Double

    open fun describe(): String = "$name with area ${area()}"
}

class Rectangle(private val w: Double, private val h: Double) : Shape("Rectangle") {
    override fun area(): Double = w * h
}

class Triangle(private val base: Double, private val height: Double) : Shape("Triangle") {
    override fun area(): Double = base * height / 2
}

class Square(private val side: Double) : Shape("Square") {
    override fun area(): Double = side * side

    override fun describe(): String = super.describe() + " (all sides " + side + ")"
}

fun main() {
    val shapes = listOf(Rectangle(4.0, 3.0), Triangle(5.0, 5.0), Square(2.0))
    for (s in shapes.sortedBy { it.area() }) println(s.describe())
    println(shapes.maxByOrNull { it.area() }?.name)
}

Quick Quiz 1 / 3

class Animal ကို open မရေးဘဲ class Dog : Animal() လို့ ရေးရင် compiler က ဘာပြောမလဲ။

Quick Quiz 2 / 3

open class A { init { println(describe()) }; open fun describe() = "A" } နဲ့ class B : A() { private val tag = "B"; override fun describe() = "tag=" + tag } ရှိရင် B() က ဘာ print ထုတ်မလဲ။

Quick Quiz 3 / 3

Abstract member အကြောင်း အမှန်က ဘာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Kotlin class နဲ့ member တွေက default final ပါ၊ parent က open လုပ်မှ child က extend နဲ့ override လုပ်လို့ရပြီး override ကို override keyword နဲ့ ရေးရပါတယ်။
  • Child က colon နောက်မှာ parent constructor ကို argument နဲ့ ခေါ်ပြီး super.member() နဲ့ parent version ကို ထပ်ခေါ်နိုင်ပါတယ်။
  • Abstract class က body မပါတဲ့ member တွေနဲ့ instance မဖန်တီးနိုင်ပြီး concrete child တိုင်းကို ဖြည့်ခိုင်းပါတယ်၊ polymorphism ကြောင့် variable type မဟုတ်ဘဲ တကယ့် object ရဲ့ version ကို run ပါတယ်။
  • Constructor ဒါမှမဟုတ် init ထဲက open function ကို မခေါ်ပါနဲ့ — child ရဲ့ property တွေ မ initialise ရသေးလို့ null ဖတ်မိပါတယ်။

Kotlin language guide — Kotlin

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Inheritance | Thuta Learning