Inheritance (parent class ကနေ အမွေဆက်ခံခြင်း)
B3.2 (HL): Implement inheritance using extends and super.
မြန်မာလို: extends နဲ့ super ကို သုံးပြီး inheritance (parent class ကနေ အမွေဆက်ခံခြင်း) ကို code နဲ့ ကိုယ်တိုင် ရေးနိုင်ရပါမယ်။
ဒီစာမျက်နှာက အကြောင်းအရာတွေကို HL အဆင့်မှာပဲ စစ်ဆေးပါတယ်။ SL (Standard Level) နဲ့ HL (Higher Level) ဆိုတာ IB Computer Science (နိုင်ငံတကာ ကျောင်းတွေမှာ သင်တဲ့ ကွန်ပျူတာသိပ္ပံ သင်ရိုး) သင်တန်းရဲ့ အဆင့်နှစ်ဆင့်ဖြစ်ပြီး HL အပိုင်းတွေက ပိုနက်နက်ရှိုင်းရှိုင်း လေ့လာရတဲ့ အပိုင်းတွေပါ။
အဓိက အယူအဆများ
Inheritance ဆိုတာ ဘာလဲ
Inheritance က class (object တွေ ဖန်တီးဖို့ ပုံစံခွက်) အသစ် တစ်ခု (child ဒါမှမဟုတ် subclass: အမွေဆက်ခံတဲ့ child class) ကို ရှိပြီးသား class တစ်ခု (parent ဒါမှမဟုတ် superclass: အမွေပေးတဲ့ parent class) ရဲ့ attribute (object ရဲ့ ဒေတာကို သိမ်းတဲ့ variable) တွေနဲ့ method (သီးခြား အလုပ်တစ်ခု လုပ်ပေးတဲ့ code အပိုင်း) တွေကို ပြန်သုံးခွင့်၊ ဆက်ချဲ့ခွင့် ပေးပါတယ်။ Child class က parent ဆီက အရာအားလုံးကို အမွေဆက်ခံပြီး attribute အသစ်တွေ၊ method အသစ်တွေ ထည့်နိုင်သလို ရှိပြီးသား behaviour (object က လုပ်နိုင်တာတွေ) ကိုလည်း ပြောင်းနိုင်ပါတယ်။
ဒါက “is-a” (class တစ်ခုက နောက် class တစ်ခုရဲ့ အမျိုးအစားခွဲ ဖြစ်တဲ့) ဆက်နွယ်မှုပါ: Dog IS-A Animal (Dog က Animal အမျိုးအစား တစ်ခု)၊ Teacher IS-A SchoolMember၊ Hare IS-A Racer။
// Parent class
public class Animal {
private String name;
private int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public int getAge() { return age; }
public String speak() {
return "...";
}
}
// Child class -- inherits name, age, getName(), getAge(), speak()
public class Dog extends Animal {
private String breed;
public Dog(String name, int age, String breed) {
super(name, age); // call the parent constructor
this.breed = breed;
}
public String getBreed() { return breed; }
// Override the parent's speak() method
@Override
public String speak() {
return "Woof!";
}
}
# Parent class
class Animal():
def __init__(self, name, age):
self._name = name # single _ marks the attribute as private
self._age = age
def get_name(self):
return self._name
def get_age(self):
return self._age
def speak(self):
return "..."
# Child class -- inherits _name, _age, get_name(), get_age(), speak()
class Dog(Animal):
def __init__(self, name, age, breed):
super().__init__(name, age) # call the parent constructor
self._breed = breed
def get_breed(self):
return self._breed
# Override the parent's speak() method
# (Python has no @Override annotation - redefining the method is enough)
def speak(self):
return "Woof!"
Dog d = new Dog("Max", 3, "Labrador");
System.out.println(d.getName()); // Max -- inherited from Animal
System.out.println(d.getAge()); // 3 -- inherited from Animal
System.out.println(d.getBreed()); // Labrador -- defined in Dog
System.out.println(d.speak()); // Woof! -- overridden in Dog
d = Dog("Max", 3, "Labrador")
print(d.get_name()) # Max -- inherited from Animal
print(d.get_age()) # 3 -- inherited from Animal
print(d.get_breed()) # Labrador -- defined in Dog
print(d.speak()) # Woof! -- overridden in Dog
Dog class က getName() ဒါမှမဟုတ် getAge() ကို ကိုယ်တိုင် မသတ်မှတ်ခဲ့ပါဘူး: Animal ဆီကနေ အမွေဆက်ခံခဲ့တာပါ။ သူက breed နဲ့ getBreed() ကို ထပ်ထည့်ခဲ့ပြီး ခွေးနဲ့ပဲ ဆိုင်တဲ့ behaviour ကို ပေးဖို့ speak() ကို override (parent ရဲ့ method ကို child က အစားထိုး ရေး) လုပ်ခဲ့ပါတယ်။
The extends Keyword (အမွေဆက်ခံကြောင်း ကြေညာတဲ့ keyword)
extends က class တစ်ခုက နောက် class တစ်ခုဆီကနေ အမွေဆက်ခံတယ်လို့ ကြေညာပေးပါတယ်:
public class Dog extends Animal { ... }
// child parent
class Dog(Animal): # child(parent) - the class in parentheses is the parent
# ...
Java code ထဲက child နဲ့ parent မှတ်ချက်တွေက Dog က child class၊ Animal က parent class ဖြစ်ကြောင်း ပြပါတယ်။ Python code ထဲက မှတ်ချက်ကတော့ ကွင်းထဲက class က parent ဖြစ်တယ်လို့ ဆိုလိုပါတယ်။
Java မှာ single inheritance (parent တစ်ခုတည်းဆီကပဲ အမွေဆက်ခံခြင်း) ကိုပဲ ခွင့်ပြုပါတယ်: class တစ်ခုက parent တစ်ခုတည်းကိုပဲ extend လုပ်နိုင်ပါတယ်။ Class နှစ်ခုကို တစ်ပြိုင်နက် extend လုပ်လို့ မရပါဘူး။ (Python မှာ extends keyword မရှိပါဘူး: class header ရဲ့ ကွင်းထဲမှာ parent ရဲ့ နာမည်ကို ရေးလိုက်တာက အတူတူ အလုပ်လုပ်ပါတယ်။)
The super Keyword (parent class ကို ညွှန်းတဲ့ keyword)
super က parent class ကို ညွှန်းပါတယ်။ အဓိက အသုံး နှစ်မျိုး ရှိပါတယ်:
1. Parent constructor (object ဖန်တီးချိန် attribute တွေကို စတင် သတ်မှတ်ပေးတဲ့ method) ကို ခေါ်ခြင်း:
public Dog(String name, int age, String breed) {
super(name, age); // calls Animal(String, int)
this.breed = breed;
}
def __init__(self, name, age, breed):
super().__init__(name, age) # call the parent constructor - Animal's __init__
self._breed = breed
super(...) ခေါ်မှုက child constructor ရဲ့ ပထမဆုံး line မဖြစ်မနေ ဖြစ်ရပါမယ်။ အမွေဆက်ခံထားတဲ့ attribute တွေကို initialise (ပထမဆုံး တန်ဖိုး ထည့်) လုပ်ဖို့ parent constructor ဆီ တန်ဖိုးတွေ ပေးလိုက်ပါတယ်။ super(...) ကို မခေါ်ရင် Java က super() ကို အလိုအလျောက် ခေါ်ပါတယ်။ super() ဆိုတာ argument (method ကို ခေါ်တဲ့အခါ တကယ် ပေးလိုက်တဲ့ တန်ဖိုး) မပါတဲ့ parent constructor ကို ခေါ်တာပါ။ Parent မှာ အဲဒီလို constructor မရှိရင် အဲဒီ ခေါ်မှုက မအောင်မြင်ပါဘူး။
2. Parent ရဲ့ method ကို ခေါ်ခြင်း:
@Override
public String speak() {
return super.speak() + " Woof!"; // calls Animal's speak() then adds to it
}
def speak(self):
return super().speak() + " Woof!" # calls Animal's speak() then adds to it
super(...)က child constructor ထဲက ပထမဆုံး statement (code ထဲက ညွှန်ကြားချက်) ဖြစ်ရပါမယ်။ ဒါက child က သူ့ကိုယ်ပိုင် attribute တွေ မထည့်ခင် object ရဲ့ parent အပိုင်းကို အရင် initialise လုပ်ပြီးသား ဖြစ်အောင် သေချာစေပါတယ်။ ဒါကို မေ့တာက အဖြစ်များတဲ့ compile error (compile လုပ်ချိန်မှာ ဖြစ်တဲ့ error) ပါ။
ဘာတွေကို အမွေဆက်ခံလဲ
Child class က ဒါတွေကို အမွေဆက်ခံပါတယ်:
publicနဲ့protectedmethod အားလုံးpublicနဲ့protectedattribute အားလုံး- Parent ရဲ့ public interface (တခြား code က သုံးလို့ရတဲ့ public method များ)
Child class က ဒါတွေကို အမွေ မဆက်ခံပါဘူး:
privateattribute တွေ: object ထဲမှာ ရှိနေပေမယ့် တိုက်ရိုက် သုံးလို့ မရပါဘူး၊ getter (private attribute ရဲ့ တန်ဖိုးကို ပြန်ပေးတဲ့ method) တွေကို သုံးပါ- Constructor တွေ: child က ကိုယ်ပိုင် constructor ကို ရေးပြီး
super(...)ကို ခေါ်ရပါမယ်
public class Animal {
private String name; // NOT directly accessible in Dog
protected int age; // accessible in Dog
public String getName() { return name; } // inherited by Dog
}
public class Dog extends Animal {
public void printInfo() {
// System.out.println(name); // ERROR -- name is private in Animal
System.out.println(getName()); // OK -- getName() is public
System.out.println(age); // OK -- age is protected
}
}
class Animal():
def __init__(self, name, age):
self._name = name # single _ = private by convention; do not touch outside Animal
self.age = age # no underscore = accessible, like Java's protected/public
def get_name(self):
return self._name # inherited by Dog
class Dog(Animal):
def print_info(self):
# print(self._name) # runs, but breaks the convention - Python does not ENFORCE privacy
print(self.get_name()) # OK -- use the parent's public getter
print(self.age) # OK -- no underscore, meant to be accessible
Code ထဲက မှတ်ချက်တွေက ဒီလို ဆိုလိုပါတယ်: Java မှာ name ကို Dog ထဲမှာ တိုက်ရိုက် သုံးရင် ERROR ဖြစ်ပြီး public ဖြစ်တဲ့ getName() နဲ့ protected ဖြစ်တဲ့ age ကိုတော့ သုံးလို့ရ (OK) ပါတယ်။ Python မှာတော့ _ တစ်လုံးနဲ့ စတဲ့ နာမည်က ထုံးစံအရ private ဖြစ်လို့ Animal ရဲ့ အပြင်မှာ မထိသင့်ပါဘူး၊ _ မပါတဲ့ နာမည်ကို Java ရဲ့ protected/public လို သုံးလို့ရပါတယ်။ self._name ကို တိုက်ရိုက် print လုပ်ရင် run တော့ run ပါတယ်၊ ဒါပေမယ့် ထုံးစံကို ချိုးဖောက်ရာ ရောက်ပါတယ်: Python က privacy ကို အတင်းအကျပ် မထိန်းပါဘူး။
Constructor Chaining (constructor တစ်ခုက နောက် constructor တစ်ခုကို ဆက်ခေါ်ခြင်း)
Dog တစ်ခုကို ဖန်တီးတဲ့အခါ Java က object ရဲ့ Animal အပိုင်းကို အရင် ဖန်တီးရပါတယ်။ ဒါကို constructor chaining လို့ ခေါ်ပါတယ်: child constructor က parent constructor ကို ခေါ်ပါတယ်။ Parent constructor က parent attribute တွေကို initialise လုပ်ပြီးမှ child constructor က သူ့ကိုယ်ပိုင် attribute တွေကို initialise လုပ်ပါတယ်။
new Dog("Max", 3, "Labrador")
│
▼
Dog constructor runs:
│
├── super("Max", 3) → Animal constructor sets name="Max", age=3
│
└── this.breed = "Labrador" → Dog constructor sets breed
ပုံထဲက စာတွေက Dog constructor runs (Dog constructor run ပါတယ်)၊ Animal constructor sets name=”Max”, age=3 (Animal constructor က name နဲ့ age ကို သတ်မှတ်ပါတယ်)၊ Dog constructor sets breed (Dog constructor က breed ကို သတ်မှတ်ပါတယ်) လို့ ဆိုလိုပါတယ်။
Inheritance ရဲ့ အားသာချက်များ
| အားသာချက် | ဘယ်လိုလဲ |
|---|---|
| Code reuse (code ကို ပြန်သုံးခြင်း) | ဘုံ attribute နဲ့ method တွေကို parent ထဲမှာ တစ်ကြိမ်ပဲ ရေးပြီး child အားလုံးက အမွေဆက်ခံပါတယ် |
| Extensibility (ဆက်ချဲ့ရလွယ်ခြင်း) | ရှိပြီးသား code ကို မပြင်ဘဲ child class အသစ်တွေ ထည့်နိုင်ပါတယ် |
| Maintainability (ပြင်ဆင် ထိန်းသိမ်းရလွယ်ခြင်း) | Parent ထဲက bug (code ထဲက အမှား) ကို ပြင်လိုက်ရင် child အားလုံးမှာ အလိုအလျောက် ပြင်ပြီးသား ဖြစ်သွားပါတယ် |
| Logical hierarchy (အဆင့်ဆင့် စနစ်တကျ ဖွဲ့စည်းပုံ) | လက်တွေ့ ကမ္ဘာက ဆက်နွယ်မှုတွေကို ထင်ဟပ်ပါတယ် (Dog is-a Animal၊ Car is-a Vehicle) |
Inheritance ရဲ့ အားနည်းချက်များ
| အားနည်းချက် | ဘယ်လိုလဲ |
|---|---|
| Tight coupling (class တွေ အချင်းချင်း အလွန် မှီခိုနေခြင်း) | Child က parent ရဲ့ implementation (တကယ် အလုပ်လုပ်အောင် ရေးထားတဲ့ code) ပေါ် မှီခိုပါတယ်၊ parent ကို ပြောင်းလိုက်ရင် child တွေ ပျက်သွားနိုင်ပါတယ် |
| Fragile base class (ပြင်ရင် ထိခိုက်လွယ်တဲ့ parent class) | Parent class ကို ပြင်လိုက်တာက subclass အားလုံးအပေါ် မရည်ရွယ်ဘဲ သက်ရောက်မှုတွေ ဖြစ်စေနိုင်ပါတယ် |
| Limited flexibility (ပြောင်းလွယ်မှု နည်းခြင်း) | Java က single inheritance ကိုပဲ ခွင့်ပြုပါတယ်: class တစ်ခုမှာ parent တစ်ခုပဲ ရှိနိုင်ပါတယ် |
Inheritance က “is-a” ဆက်နွယ်မှုတွေကို ဖန်တီးပေးပြီး code reuse၊ extensibility၊ debugging (program ထဲက အမှားကို ရှာပြီး ပြင်ခြင်း) ပိုလွယ်ခြင်းနဲ့ maintainability တို့ကို ဖြစ်စေပါတယ်။
UML Notation (UML ပုံထဲမှာ ပြပုံ)
UML (class တွေကို ပုံနဲ့ ပြတဲ့ စံ ရေးဆွဲနည်း) မှာ inheritance ကို မျဉ်းအပြည့် (solid line) တစ်ကြောင်းနဲ့ parent ဘက်ကို ညွှန်ထားတဲ့ ပိတ်ထားတဲ့ တြိဂံ (closed triangle) နဲ့ ပြပါတယ်:
classDiagram
class Animal {
-name : String
-age : int
+getName()
+speak()
}
class Dog {
-breed : String
+getBreed()
+speak()
}
Animal <|-- Dog
အဆင့်ဆင့် ရှင်းပြထားတဲ့ ဥပမာများ
ဥပမာ 1: Class Hierarchy (class တွေကို အဆင့်ဆင့် ဖွဲ့ထားတဲ့ ပုံစံ) တည်ဆောက်ခြင်း
အခြေအနေ: ကျောင်းတစ်ကျောင်းမှာ လူ အမျိုးအစား အမျိုးမျိုး ရှိပါတယ်: ကျောင်းသားတွေနဲ့ ဆရာတွေ။ နှစ်မျိုးလုံးမှာ နာမည်နဲ့ အသက် ရှိပါတယ်။ ကျောင်းသားတွေမှာ grade (အတန်း) ရှိပြီး ဆရာတွေမှာ subject (သင်တဲ့ ဘာသာရပ်) ရှိပါတယ်။
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public int getAge() { return age; }
}
public class Student extends Person {
private int grade;
public Student(String name, int age, int grade) {
super(name, age);
this.grade = grade;
}
public int getGrade() { return grade; }
}
public class Teacher extends Person {
private String subject;
public Teacher(String name, int age, String subject) {
super(name, age);
this.subject = subject;
}
public String getSubject() { return subject; }
}
class Person():
def __init__(self, name, age):
self._name = name
self._age = age
def get_name(self):
return self._name
def get_age(self):
return self._age
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age) # call the parent constructor
self._grade = grade
def get_grade(self):
return self._grade
class Teacher(Person):
def __init__(self, name, age, subject):
super().__init__(name, age) # call the parent constructor
self._subject = subject
def get_subject(self):
return self._subject
Student s = new Student("Alice", 16, 11);
Teacher t = new Teacher("Mr. Smith", 35, "CS");
System.out.println(s.getName() + " Grade " + s.getGrade());
System.out.println(t.getName() + " teaches " + t.getSubject());
s = Student("Alice", 16, 11)
t = Teacher("Mr. Smith", 35, "CS")
print(s.get_name(), "Grade", s.get_grade())
print(t.get_name(), "teaches", t.get_subject())
Output (program က ထုတ်ပြတဲ့ ရလဒ်):
Alice Grade 11
Mr. Smith teaches CS
Student နဲ့ Teacher နှစ်ခုလုံးက Person ဆီကနေ getName() နဲ့ getAge() ကို အမွေဆက်ခံပါတယ်။ တစ်ခုစီက သူ့ကိုယ်ပိုင် attribute နဲ့ getter ကို ထပ်ထည့်ပါတယ်။
ဥပမာ 2: Racer Hierarchy
IB သင်တန်းတွေမှာ ကြာမြင့်စွာ သုံးလာတဲ့ သင်ကြားရေး ဥပမာ တစ်ခုကို အခြေခံထားပါတယ်။ Racer (ပြိုင်ပွဲဝင်) ရဲ့ child တွေက Hare (ယုန်) နဲ့ Tortoise (လိပ်) ပါ:
public class Racer {
private String name;
private int position;
public Racer(String name) {
this.name = name;
this.position = 0;
}
public String getName() { return name; }
public int getPosition() { return position; }
public void move() {
position = position + 1; // default: move 1 step
}
}
public class Hare extends Racer {
public Hare(String name) {
super(name);
}
@Override
public void move() {
// 30% chance of jumping 5 steps, 70% chance of sleeping
if (Math.random() < 0.3) {
// Jump forward 5 positions
for (int i = 0; i < 5; i++) {
super.move(); // reuse parent's move()
}
}
// else: do nothing (sleep)
}
}
public class Tortoise extends Racer {
public Tortoise(String name) {
super(name);
}
@Override
public void move() {
super.move(); // always moves exactly 1 step
}
}
import random
class Racer():
def __init__(self, name):
self._name = name
self._position = 0
def get_name(self):
return self._name
def get_position(self):
return self._position
def move(self):
self._position = self._position + 1 # default: move 1 step
class Hare(Racer):
def __init__(self, name):
super().__init__(name) # call the parent constructor
def move(self):
# 30% chance of jumping 5 steps, 70% chance of sleeping
if random.random() < 0.3: # random.random() is Python's Math.random()
# Jump forward 5 positions
for i in range(5):
super().move() # reuse parent's move()
# else: do nothing (sleep)
class Tortoise(Racer):
def __init__(self, name):
super().__init__(name)
def move(self):
super().move() # always moves exactly 1 step
Code ထဲက မှတ်ချက်တွေအရ Hare က move() ကို ခေါ်တိုင်း 30% အခွင့်အရေးနဲ့ 5 လှမ်း ခုန်ပြီး 70% အခွင့်အရေးနဲ့ အိပ်နေပါတယ် (ဘာမှ မလုပ်ပါဘူး)။ Tortoise ကတော့ အမြဲ 1 လှမ်းတိတိ ရွှေ့ပါတယ်။
Hare နဲ့ Tortoise နှစ်ခုလုံး IS-A Racer ဖြစ်ပေမယ့် move() ကို မတူတဲ့ နည်းတွေနဲ့ override လုပ်ပါတယ်။ ဒါက polymorphism (ပုံစံ အမျိုးမျိုး ယူနိုင်ခြင်း) အတွက် အခြေခံ ဖြစ်ပါတယ် (နောက် စာမျက်နှာမှာ ရှင်းပြထားပါတယ်)။
Quick Check (အမြန် စစ်ဆေးမေးခွန်း)
Q1. Program တစ်ခုထဲမှာ ဒီ class header (class ကို ကြေညာတဲ့ ပထမ line) ပါပါတယ်။ ဒီ header နဲ့ ပတ်သက်ပြီး ဘယ်အဆိုက မှန်သလဲ။
public class Ferry extends Vessel { Q2. Child constructor ထဲမှာ super(...) က ဘယ်နေရာမှာ ရှိရမလဲ။
Q3. Child class က parent ရဲ့ private attribute တွေကို တိုက်ရိုက် သုံးလို့ ရသလား။
Q4. ဘယ်ဟာက inheritance ရဲ့ အားသာချက်လဲ။
Q5. ဘယ်ဟာက inheritance ရဲ့ အားနည်းချက်လဲ။
Trace Exercise (code ကို တစ်ဆင့်ချင်း လိုက်ကြည့်ခြင်း)
Student object တစ်ခုကို ဖန်တီးပုံကို trace (တစ်ဆင့်ချင်း လိုက်ကြည့်ပြီး မှတ်) လုပ်ပါ။ Constructor chaining ဘယ်လို အလုပ်လုပ်လဲ လိုက်ကြည့်ပါ။ အဖြေတွေကို code ထဲကအတိုင်း English လို ရိုက်ထည့်ပါ (Constructor ကော်လံမှာ class နာမည်ကို ရိုက်ပါ)။
Trace: Constructor Chaining
Student s = new Student("Alice", 16, 11);အစဉ်လိုက် ဘာတွေ ဖြစ်သလဲ။
| အဆင့် | Constructor | name | age | grade |
|---|---|---|---|---|
| 1. super("Alice", 16) ကို ခေါ်တယ် | -- | |||
| 2. this.grade = 11 |
Spot the Error (အမှား ရှာခြင်း)
ဒီ code က compile မဖြစ်ပါဘူး။ Constructor ထဲမှာ မဖြစ်မနေ ပါရမယ့် ခေါ်မှု တစ်ခု ကျန်နေပါတယ်။ ကျန်နေတဲ့ statement ကို ထည့်ရမယ့် line ကို ရွေးပါ၊ ပြီးရင် ပြင်နည်းကို ရွေးပါ။
this.grade = grade; ရဲ့ ရှေ့မှာ ဘာထည့်ရမလဲ ရွေးပါ:
Predict the Output (output ကို ကြိုခန့်မှန်းခြင်း)
ဒီ code က ဘာကို print ထုတ်သလဲ။
Dog d = new Dog("Max", 3, "Labrador");
System.out.println(d.getName() + " says " + d.speak() + " (" + d.getBreed() + ")"); Practice Exercises (လေ့ကျင့်ခန်းများ)
Core (အခြေခံ အဆင့်)
-
Hierarchy တစ်ခု တည်ဆောက်ပါ:
make(String)၊year(int) နဲ့getDescription()ပါတဲ့Vehicleparent class တစ်ခုကို ဖန်တီးပါ။ Child class နှစ်ခု ဖန်တီးပါ:Car(numDoorsကို ထပ်ထည့်) နဲ့Motorcycle(hasSidecarကို ထပ်ထည့်၊ true ဒါမှမဟုတ် false တန်ဖိုးပဲ ယူတဲ့ boolean ပါ)။ Child တစ်ခုစီက သူ့ constructor ထဲမှာsuper(...)ကို ခေါ်ရပါမယ်။ - Inheritance ကို ခွဲခြားပါ: အတွဲ တစ်တွဲစီအတွက် inheritance က သင့်တော်ရဲ့လား State (ဖော်ပြပါ) လုပ်ပြီး ဘာကြောင့်လဲ Explain (ရှင်းပြပါ) လုပ်ပါ:
- (a)
Textbookနဲ့Book - (b)
Laptopနဲ့Battery - (c)
Teacherနဲ့Person - (d)
Playlistနဲ့Song
- (a)
- Constructor chaining ကို trace လုပ်ပါ:
Vehicle->Car->ElectricCarဆိုတဲ့ hierarchy ကို ပေးထားပါတယ်။ 350 က battery နဲ့ သွားနိုင်တဲ့ အကွာအဝေး (km) ဖြစ်တဲ့ElectricCar("Tesla", 2024, 4, 350)ကို ဖန်တီးတဲ့အခါ ဘာဖြစ်လဲ trace လုပ်ပါ။ ဘယ် constructor က အရင်ဆုံး run လဲ ပြပါ။
Extension (တိုးချဲ့ အဆင့်)
-
Method ကို override လုပ်ပါ: parent ရဲ့ version ကို override လုပ်တဲ့
getDescription()method ကိုCarနဲ့Motorcycleနှစ်ခုလုံးမှာ ထည့်ပါ။ Car ရဲ့ method မှာ တံခါး အရေအတွက် ပါရပါမယ်၊ Motorcycle ရဲ့ method မှာ sidecar (ဘေးတွဲ ထိုင်ခုံ) ပါမပါ ဖော်ပြရပါမယ်။ Parent ရဲ့ output ကိုပါ ထည့်ဖို့super.getDescription()ကို သုံးပါ။ -
အားသာချက်နဲ့ အားနည်းချက်များ: Class နှစ်ခုမှာ attribute တစ်ခုခု တူနေတိုင်း inheritance ကို သုံးသင့်တယ်လို့ ကျောင်းသား တစ်ယောက်က ပြောပါတယ်။ ဒါ ဘာကြောင့် မှားလဲ Explain လုပ်ပါ။ Class တွေမှာ တူတဲ့ attribute တွေ ရှိပေမယ့် aggregation (သီးခြား ရှိနေနိုင်တဲ့ has-a ဆက်နွယ်မှု) က ပိုသင့်တော်တဲ့ ဥပမာ တစ်ခုလည်း ပေးပါ။
Challenge (စိန်ခေါ်မှု အဆင့်)
- Inheritance ပါတဲ့ စနစ် အပြည့်အစုံ: ဖုန်းရဲ့ data plan (အင်တာနက် သုံးဖို့ ဝယ်ထားတဲ့ ဒေတာ ပမာဏ) စနစ်တစ်ခုကို design ဆွဲပါ။ Class သုံးခု ပါပါတယ်။
DataPlanက parent ပါ: ပိုင်ရှင်နဲ့ ကျန်နေတဲ့ megabyte ပမာဏကို သိမ်းပြီးaddData()နဲ့ ကျန်နေတာထက် ပိုသုံးခွင့် မပေးတဲ့useData()ပါပါတယ်။BonusPlanက bonus rate (ဒေတာ အပို ထပ်ပေးတဲ့ နှုန်း) နဲ့addBonus()method ကို ထပ်ထည့်ပါတယ်။FlexPlanက extra-data limit (ကျန်တာထက် ပိုသုံးခွင့်ရတဲ့ ကန့်သတ်ချက်) နဲ့ ပြင်ထားတဲ့useData()ကို ထပ်ထည့်ပါတယ်: ဒီuseData()က ကျန်နေတဲ့ megabyte ကို သုညအောက် ရောက်ခွင့် ပေးပါတယ်၊ အဲဒီ limit ရဲ့ အနုတ်တန်ဖိုးအထိပါ။ UML ပုံကို ဆွဲပြီး class သုံးခုလုံးကို ရေးပါ။
ဆက်စပ် ခေါင်းစဉ်များ
- အရင် လေ့လာထားရမယ့် ခေါင်းစဉ်: Classes and Objects (class နဲ့ object များ): attribute တွေနဲ့ method တွေ ပါတဲ့ class တွေကို ဖန်တီးခြင်း
- အရင် လေ့လာထားရမယ့် ခေါင်းစဉ်: Constructors (object ဖန်တီးချိန် attribute တွေကို စတင် သတ်မှတ်ပေးတဲ့ method များ): constructor တွေက object တွေကို ဘယ်လို initialise လုပ်လဲ နားလည်ခြင်း
- အရင် လေ့လာထားရမယ့် ခေါင်းစဉ်: Aggregation (သီးခြား ရှိနေနိုင်တဲ့ has-a ဆက်နွယ်မှု): has-a နဲ့ is-a ကို ဘယ်အချိန် သုံးရမလဲ နားလည်ခြင်း
- နောက်ထပ် လေ့လာမယ့် ခေါင်းစဉ်: Method Overriding (parent ရဲ့ method ကို child က အစားထိုး ရေးခြင်း):
@Overrideနဲ့ parent ရဲ့ behaviour ကို ပြောင်းခြင်းအကြောင်း ပိုနက်နက် လေ့လာခြင်း - နောက်ထပ် လေ့လာမယ့် ခေါင်းစဉ်: Polymorphism (ပုံစံ အမျိုးမျိုး ယူနိုင်ခြင်း)။ Child object တွေကို parent type reference တွေနဲ့ ကိုင်ထားခြင်း