Java: this နဲ့ super

Cing Sian Dal
1 min readJul 16, 2020

Inheritance မှာ this နဲ့ super ဘယ်လို အသုံးဝင်သလဲ?

class Parent{

int age = 18; // default parent age

}

class Child extends Parent{

int age = 0; // default child age

Child(int childAge, int parentAge){
super.age = parentAge; // "super" as constructor
this.age = childAge; // "this" as constructor
}

void print(){
System.out.println(super.age); // "super" as variable
System.out.println(this.age); // "this" as variable
}

}


public class Main{

public static void main(String[] args){
Child c = new Child(60,10);
c.print(); // output: 10, 60
}

}
  • this က မိမိရဲ့ class အတွက်မှာ ရှိတဲ့ state နဲ့ behaviour အပါအဝင် extend လုပ်ထားတဲ့ class ရဲ့ state နဲ့ behaviour တွေကိုပါ ဆွဲယူ အသုံးပြုလို့ရပါတယ်။
  • super ဆိုတာက မိခင် class ထဲက state နဲ့ behaviour ကို ဆွဲယူ အသုံးပြုချင်တဲ့အခါမှာ အသုံးပြုပါတယ်။

--

--

Cing Sian Dal

Don’t follow me. I wrote junks here. Follow me on Twitter instead.