Java သည် Keyword ကို ချဲ့ထွင်သည်။
ဥပမာ
Car
class (subclass) သည် class (superclass) မှ attribute များနှင့် method များကို အမွေဆက်ခံ သည် Vehicle
-
class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
}
class Car extends Vehicle {
private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {
// Create a myCar object
Car myCar = new Car();
// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();
// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);
}
}
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
သော့ချက်စကားလုံးသည် အတန်းတစ်ခုကို တိုးချဲ့သည် ( extends
အတန်းတစ်ခုကို အခြားအတန်းမှ အမွေဆက်ခံကြောင်း ဖော်ပြသည်)။
Java တွင်၊ class တစ်ခုမှ အခြားတစ်ခုသို့ attribute များနှင့် method များကို အမွေဆက်ခံနိုင်သည်။ ကျွန်ုပ်တို့သည် "အမွေဆက်ခံခြင်းသဘောတရား" ကို အမျိုးအစားနှစ်မျိုးဖြင့် အုပ်စုဖွဲ့သည်-
- အတန်းခွဲ (ကလေး) - အခြားအတန်းမှ အမွေဆက်ခံသော အတန်း
- superclass (မိဘ) - ထံမှအမွေဆက်ခံခံရသောအတန်း
အတန်းတစ်ခုမှ အမွေဆက်ခံရန် extends
သော့ချက်စကားလုံးကို အသုံးပြုပါ။
ဆက်စပ်စာမျက်နှာများ
ကျွန်ုပ်တို့၏ Java Inheritance Tutorial တွင် အမွေဆက်ခံခြင်းအကြောင်း ပိုမိုဖတ်ရှုပါ ။