OOP Terminology Reference (OOP ဝေါဟာရ ကိုးကားစာရင်း)
B3.1, B3.2: Every OOP term in these two sections, with precise definitions and code examples.
မြန်မာလို: IB Computer Science (နိုင်ငံတကာ ကျောင်းတွေမှာ သင်တဲ့ ကွန်ပျူတာသိပ္ပံ သင်ရိုး) syllabus ရဲ့ B3.1 နဲ့ B3.2 အပိုင်း နှစ်ခုထဲက OOP (object တွေကို အခြေခံပြီး program ရေးနည်း) ဝေါဟာရ အားလုံးကို တိကျတဲ့ အဓိပ္ပာယ်တွေ၊ code ဥပမာတွေနဲ့ စုထားပါတယ်။
- Class နဲ့ object: class (object တွေ ဖန်တီးဖို့ ပုံစံခွက်) က blueprint (ပုံစံ ချထားတဲ့ ပုံကြမ်း) ပါ။ Object (class တစ်ခုကနေ ဖန်တီးထားတဲ့ instance) တစ်ခုကို အဲဒီ class ကနေ
newနဲ့ ဖန်တီးပါတယ်။- Encapsulation (ဒေတာနဲ့ method ကို တစ်စုတည်း ထုပ်ပိုးခြင်း): attribute (object ရဲ့ ဒေတာကို သိမ်းတဲ့ variable) တွေကို private (class ပြင်ပကနေ တိုက်ရိုက် သုံးလို့ မရ) လုပ်ထားပြီး public getter (private attribute ရဲ့ တန်ဖိုးကို ပြန်ပေးတဲ့ method) နဲ့ setter (private attribute ရဲ့ တန်ဖိုးကို ပြောင်းတဲ့ method) တွေကနေတစ်ဆင့်ပဲ ယူသုံးရပါတယ်။ ဒီနောက်မှာ
this၊ static member (object တစ်ခုချင်းစီ မဟုတ်ဘဲ class ကိုယ်တိုင် ပိုင်တဲ့ variable နဲ့ method)၊ reference (object ရှိရာ memory နေရာကို ညွှန်းတဲ့ တန်ဖိုး)၊ UML class diagram (class တစ်ခုကို အကွက် သုံးကွက်နဲ့ ပြတဲ့ ပုံ) နဲ့ aggregation (သီးခြား ရှိနေနိုင်တဲ့ has-a ဆက်နွယ်မှု) တို့ လာပါတယ်။- Inheritance (parent class ကနေ အမွေဆက်ခံခြင်း): HL (Higher Level: IB သင်တန်းမှာ ပိုနက်နက်ရှိုင်းရှိုင်း လေ့လာရတဲ့ အဆင့်) ဝေါဟာရတွေ ဒီကနေ စပါတယ်။ Subclass (အမွေဆက်ခံတဲ့ child class) တစ်ခုက superclass (အမွေပေးတဲ့ parent class) တစ်ခုကို extend (အမွေဆက်ခံ) လုပ်တာပါ။ ဒီနောက်မှာ polymorphism (ပုံစံ အမျိုးမျိုး ယူနိုင်ခြင်း)၊ abstraction (မလိုတဲ့ အသေးစိတ်ကို ဖယ်ပြီး အဓိကကိုပဲ ကြည့်ခြင်း)၊ composition (ပိုခိုင်မာတဲ့ has-a ဆက်နွယ်မှု) နဲ့ design pattern (ထပ်ကြုံရတဲ့ ဒီဇိုင်း ပြဿနာအတွက် ဖြေရှင်းပုံစံ) သုံးမျိုး လာပါတယ်။
- အမြန် ကြည့်ရန် ဇယား: အဓိက ဝေါဟာရတွေကို တစ်ခုကို တစ်ကြောင်းစီနဲ့ ပြထားပါတယ်။
ခြုံငုံ မိတ်ဆက်
Object-oriented programming အကြောင်း မေးခွန်းတွေမှာ Define (အဓိပ္ပာယ်ကို တိတိကျကျ ဖော်ပြပါ)၊ Distinguish (အရာ နှစ်ခု ဒါမှမဟုတ် ပိုများတဲ့ အရာတွေကြားက ကွာခြားချက်ကို ရှင်းရှင်းလင်းလင်း ပြပါ) နဲ့ Explain (ရှင်းပြပါ) လို command term (မေးခွန်းက ဘာလုပ်ခိုင်းလဲ ပြတဲ့ IB ရဲ့ သတ်မှတ် စကားလုံး) တွေကို သုံးပါတယ်။ အယူအဆ မှန်ပေမဲ့ စကားလုံး မတိကျရင် အဲဒီ အဖြေက အမှတ် လွတ်သွားနိုင်ပါတယ်။ ဒီစာမျက်နှာမှာ B3.1 နဲ့ B3.2 ထဲက OOP ဝေါဟာရ အားလုံးကို တိကျတဲ့ အဓိပ္ပာယ်နဲ့ စုထားပါတယ်။
ဒီစာမျက်နှာကို ပြန်လေ့လာဖို့ (revision) သုံးပါ။ ဝေါဟာရ တစ်ခုစီမှာ ဒါတွေ ပါပါတယ်:
- တိကျတဲ့ အဓိပ္ပာယ် (မှတ်ထားရမယ့် ရေးပုံ)
- ဝေါဟာရကို လက်တွေ့ သုံးပြတဲ့ code snippet (code အပိုင်းအစ)
- လိုအပ်တဲ့ နေရာတွေမှာ စာမေးပွဲအတွက် အကြံပြုချက်များ
Class နဲ့ Object (B3.1)
Class (object တွေ ဖန်တီးဖို့ ပုံစံခွက်)
Class ဆိုတာ သူ့ကနေ ဖန်တီးမယ့် object တွေမှာ ရှိရမယ့် attribute (ဒေတာ) တွေနဲ့ method (လုပ်ဆောင်ပုံ) တွေကို သတ်မှတ်ပေးတဲ့ template/blueprint (ပုံစံခွက်/ပုံကြမ်း) ပါ။ Class ကိုယ်တိုင်က ဒေတာ မသိမ်းပါဘူး: object တွေ လိုက်နာရမယ့် ဖွဲ့စည်းပုံ ကိုပဲ ဖော်ပြပါတယ်။
public class Dog {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public void bark() { System.out.println(name + " says Woof!"); }
}
class Dog():
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 bark(self):
print(self._name, "says Woof!")
မှတ်ထားရမယ့် အဓိပ္ပာယ်: “Class ဆိုတာ အဲဒီ အမျိုးအစား object အားလုံးမှာ တူတူ ရှိတဲ့ attribute တွေနဲ့ method တွေကို သတ်မှတ်ပေးတဲ့ blueprint/template ပါ။”
Object (class တစ်ခုကနေ ဖန်တီးထားတဲ့ instance)
Object ဆိုတာ program run နေစဉ် (runtime) ဖန်တီးလိုက်တဲ့ class တစ်ခုရဲ့ သီးခြား instance တစ်ခုပါ။ Object တစ်ခုစီမှာ class က သတ်မှတ်ထားတဲ့ instance variable (object တစ်ခုချင်းရဲ့ ကိုယ်ပိုင် variable) တွေရဲ့ ကိုယ်ပိုင် copy ရှိပါတယ်။
Dog rex = new Dog("Rex", 5);
Dog bella = new Dog("Bella", 3);
// rex and bella are two separate objects of the Dog class
rex = Dog("Rex", 5)
bella = Dog("Bella", 3)
# rex and bella are two separate objects of the Dog class
# (Python has no new keyword - calling the class name creates the object)
မှတ်ထားရမယ့် အဓိပ္ပာယ်: “Object ဆိုတာ new keyword (programming language မှာ အထူး အဓိပ္ပာယ်ရှိတဲ့ စကားလုံး) နဲ့ ဖန်တီးထားပြီး ကိုယ်ပိုင် attribute တန်ဖိုးတွေ ရှိတဲ့ class တစ်ခုရဲ့ instance တစ်ခုပါ။”
Instantiation (class ကနေ object အသစ် ဖန်တီးခြင်း)
new keyword ကို သုံးပြီး class တစ်ခုကနေ object တစ်ခု ဖန်တီးတဲ့ လုပ်ငန်းစဉ်ပါ။ ဒီလုပ်ငန်းစဉ်က object အတွက် memory (မှတ်ဉာဏ်) နေရာ ချပေးပြီး object ရဲ့ constructor ကို ခေါ်ပါတယ်။
Dog rex = new Dog("Rex", 5); // instantiation
rex = Dog("Rex", 5) # instantiation - Python needs no new keyword
Constructor (object ဖန်တီးချိန် attribute တွေကို စတင် သတ်မှတ်ပေးတဲ့ method)
new နဲ့ object ဖန်တီးတဲ့အခါ အလိုအလျောက် ခေါ်တဲ့ အထူး method ပါ။ Object ရဲ့ attribute တွေထဲကို မှန်ကန်တဲ့ စတင် တန်ဖိုးတွေ ထည့်ပေးပါတယ်။
public Dog(String name, int age) { // constructor -- same name as class, no return type
this.name = name;
this.age = age;
}
def __init__(self, name, age): # constructor - in Python it is always named __init__
self._name = name
self._age = age
အဓိက အချက်များ:
- Class နာမည်နဲ့ နာမည် တူပါတယ်
- Return type (method က ပြန်ပေးတဲ့ ဒေတာ အမျိုးအစား) မရှိပါဘူး (
voidတောင် မပါပါဘူး) newက အလိုအလျောက် ခေါ်ပါတယ်- Overload လုပ်လို့ ရပါတယ်: parameter (method ကို ပေးလိုက်တဲ့ တန်ဖိုးကို လက်ခံတဲ့ variable) list မတူတဲ့ constructor အများကြီး ရှိနိုင်ပါတယ်
Dot Notation (object နောက်မှာ အစက် ခံပြီး ယူသုံးတဲ့ ရေးပုံ)
Object ရဲ့ method တွေနဲ့ (မြင်ခွင့်ရှိရင်) attribute တွေကို ယူသုံးဖို့ ရေးတဲ့ ရေးထုံးပါ: object.method() ဒါမှမဟုတ် object.attribute။
rex.bark(); // call a method
String n = rex.getName(); // call a getter
rex.bark() # call a method
n = rex.get_name() # call a getter
Encapsulation နဲ့ Information Hiding (B3.1.5)
Encapsulation (ဒေတာနဲ့ method ကို တစ်စုတည်း ထုပ်ပိုးခြင်း)
Object ရဲ့ ဒေတာ (attribute) တွေနဲ့ အဲဒီ ဒေတာပေါ်မှာ အလုပ်လုပ်တဲ့ method တွေကို unit (အစု) တစ်ခုတည်း (class တစ်ခု) ထဲမှာ စုထုပ်ပြီး အပြင်ကနေ ဒေတာကို တိုက်ရိုက် ယူသုံးတာကို ကန့်သတ်ထားတာပါ။
မှတ်ထားရမယ့် အဓိပ္ပာယ်: “Encapsulation ဆိုတာ ဒေတာနဲ့ method တွေကို unit တစ်ခုတည်းထဲ စုထုပ်ပြီး attribute တွေကို private လုပ်ကာ အဲဒီ attribute တွေကို ယူသုံးဖို့ ဒါမှမဟုတ် ပြင်ဖို့ public method တွေ ပေးခြင်းအားဖြင့် အတွင်းပိုင်း ဒေတာကို တိုက်ရိုက် ယူသုံးတာကို ကန့်သတ်တာပါ။”
Information Hiding (object ရဲ့ အတွင်း ဒေတာကို ဖုံးထားခြင်း)
Object ရဲ့ အတွင်းပိုင်း state (object ထဲမှာ လက်ရှိ ရှိနေတဲ့ တန်ဖိုးများ) ကို အပြင်က code က မမြင်ရအောင် လုပ်ထားတာပါ။ အပြင်က code က field (object ထဲက အချက်အလက် အကွက်) တွေကို တိုက်ရိုက် မယူသုံးနိုင်ဘဲ object ရဲ့ public method တွေကနေတစ်ဆင့်ပဲ object ကို သုံးနိုင်ပါတယ်။ ဒါက data integrity (ဒေတာ မှန်ကန်ပြီး မပျက်စီးဘဲ ရှိနေခြင်း) ကို ကာကွယ်ပေးပါတယ်။
လက်တွေ့မှာ information hiding ကို private field တွေနဲ့ public getter/setter method တွေနဲ့ လုပ်ပါတယ်။
Access Modifiers (ဘယ်ကနေ သုံးခွင့်ရလဲ သတ်မှတ်တဲ့ keyword များ)
Program ရဲ့ ဘယ်အပိုင်းတွေက class member (class ထဲက attribute ဒါမှမဟုတ် method) တစ်ခုကို ယူသုံးခွင့် ရလဲဆိုတာ ထိန်းချုပ်တဲ့ keyword တွေပါ။ အောက်က ဇယားမှာ “ရ” ဆိုတာ ယူသုံးခွင့် ရှိတယ်လို့ ဆိုလိုပါတယ် (package ဆိုတာ ဆက်စပ်တဲ့ class တွေကို စုထားတဲ့ အုပ်စုပါ)။
| Modifier | Class တူ | Package တူ | Subclass | နေရာတိုင်း |
|---|---|---|---|---|
private | ရ | မရ | မရ | မရ |
| default (keyword မပါ) | ရ | ရ | မရ | မရ |
protected | ရ | ရ | ရ | မရ |
public | ရ | ရ | ရ | ရ |
စာမေးပွဲအတွက် မှတ်ထားစရာ: attribute တွေက private၊ method တွေက public၊ helper method (တခြား method တွေက ခေါ်သုံးဖို့ ရေးထားတဲ့ အကူ method) တွေက private ပါ။
Accessor (Getter) Method (private attribute ရဲ့ တန်ဖိုးကို ပြန်ပေးတဲ့ method)
Private attribute တစ်ခုရဲ့ တန်ဖိုးကို မပြောင်းဘဲ ပြန်ပေးတဲ့ public method ပါ။
public String getName() {
return name;
}
def get_name(self):
return self._name
Mutator (Setter) Method (private attribute ရဲ့ တန်ဖိုးကို ပြောင်းတဲ့ method)
Private attribute တစ်ခုရဲ့ တန်ဖိုးကို ပြောင်းပေးတဲ့ public method ပါ။ Validation (ထည့်ပေးတဲ့ ဒေတာ မှန်မမှန် စစ်ခြင်း) ကို မကြာခဏ ထည့်ပါတယ်။
public void setAge(int age) {
if (age >= 0) {
this.age = age;
}
}
def set_age(self, age):
if age >= 0:
self._age = age
Setter တွေမှာ validation ဘာကြောင့် အရေးကြီးလဲ: Validation မပါရင် အပြင်က code က မမှန်တဲ့ တန်ဖိုးတွေ (ဥပမာ အနုတ်ကိန်း အသက်) ကို ထည့်နိုင်ပါတယ်။ Setter တွေက ဂိတ်စောင့်တွေလို အလုပ်လုပ်ပါတယ်။
this Keyword (B3.1)
this (လက်ရှိ object ကို ညွှန်းတဲ့ keyword)
လက်ရှိ object ကို ညွှန်းတဲ့ reference ပါ: ဆိုလိုတာက လက်ရှိ run နေတဲ့ method ဒါမှမဟုတ် constructor ကို ပိုင်တဲ့ object ပါ။ Parameter shadowing (နာမည်တူ parameter က instance variable ကို ဖုံးထားခြင်း) ကို ဖြေရှင်းဖို့ အသုံးအများဆုံးပါ: constructor/method ရဲ့ parameter တစ်ခုက instance variable တစ်ခုနဲ့ နာမည် တူနေတဲ့ အခြေအနေပါ။
public Dog(String name, int age) {
this.name = name; // this.name = instance variable, name = parameter
this.age = age;
}
def __init__(self, name, age):
self._name = name # self._name = attribute, name = parameter (self is Python's this)
self._age = age
Static Member နဲ့ Instance Member (B3.1.3)
Instance Variable (object တစ်ခုချင်းရဲ့ ကိုယ်ပိုင် variable)
Object တစ်ခုချင်းစီက ပိုင်တဲ့ variable ပါ။ Object တိုင်းမှာ ကိုယ်ပိုင် copy ရှိပါတယ်။
private String name; // each Dog has its own name
self._name = name # created in __init__ - each Dog object has its own name
Static Variable (Class Variable: class တစ်ခုလုံး မျှသုံးတဲ့ variable)
Class ရဲ့ object အားလုံး မျှသုံးတဲ့ variable ပါ။ Object ဘယ်နှစ်ခု ဖန်တီးဖန်တီး copy တစ်ခုတည်းပဲ ရှိပါတယ်။
private static int dogCount = 0; // shared across all Dog objects
class Dog():
dog_count = 0 # class attribute: defined at class level, shared by all Dog objects
Instance Method (object တစ်ခုရဲ့ ဒေတာပေါ်မှာ အလုပ်လုပ်တဲ့ method)
သီးခြား object တစ်ခုရဲ့ ဒေတာပေါ်မှာ အလုပ်လုပ်တဲ့ method ပါ။ Instance variable တွေရော static variable တွေကိုပါ ယူသုံးနိုင်ပါတယ်။
public String getName() { return name; } // acts on this object's name
def get_name(self):
return self._name # acts on this object's name
Static Method (class ကိုယ်တိုင် ပိုင်တဲ့ method)
Object တစ်ခုချင်းစီ မဟုတ်ဘဲ class ကိုယ်တိုင် ပိုင်တဲ့ method ပါ။ Instance variable တွေကို ယူသုံးလို့ မရသလို this ကိုလည်း သုံးလို့ မရပါဘူး။
public static int getDogCount() {
return dogCount; // can only access static variables
}
def get_dog_count(): # a plain function - Python has no static keyword
return Dog.dog_count # class attributes are read through the class name
မှတ်ထားရမယ့် အဓိပ္ပာယ်: “Static method ဆိုတာ instance တစ်ခုက မဟုတ်ဘဲ class က ပိုင်တဲ့ method ပါ။ Object မဖန်တီးဘဲ ခေါ်လို့ ရပြီး instance variable တွေကို ယူသုံးလို့ မရပါဘူး။”
အဖြစ်များတဲ့ အမှား: static method ထဲမှာ this.dogCount လို့ ရေးမိတာပါ။ Static method တွေမှာ this မရှိပါဘူး: သူတို့က ဘယ် object ရဲ့ method မှ မဟုတ်ပါဘူး။
Object Reference များ (B3.1)
Reference (object ရှိရာ memory နေရာကို ညွှန်းတဲ့ တန်ဖိုး)
Object ကိုယ်တိုင်ကို မဟုတ်ဘဲ object ရဲ့ memory address (memory ထဲက နေရာ လိပ်စာ) ကို သိမ်းထားတဲ့ variable ပါ။ Object variable တစ်ခုကို နောက် variable တစ်ခုထဲ assign (variable ထဲ တန်ဖိုး ထည့်) လုပ်တဲ့အခါ object ကို မဟုတ်ဘဲ reference ကို copy ကူးတာပါ။
Dog a = new Dog("Rex", 5);
Dog b = a; // b points to the SAME object as a
b.bark(); // "Rex says Woof!" -- same object
a = Dog("Rex", 5)
b = a # b points to the SAME object as a (Python variables hold references too)
b.bark() # Rex says Woof! -- same object
Aliasing (variable နှစ်ခုက object တစ်ခုတည်းကို ညွှန်းနေခြင်း)
Variable နှစ်ခု ဒါမှမဟုတ် ပိုများတဲ့ variable တွေက memory ထဲက object တစ်ခုတည်း ကို ညွှန်းနေတဲ့ အခြေအနေပါ။ Variable တစ်ခုကနေ လုပ်လိုက်တဲ့ ပြောင်းလဲမှုကို နောက် variable ကနေလည်း မြင်ရပါတယ်။
Dog a = new Dog("Rex", 5);
Dog b = a; // aliasing -- a and b point to the same Dog
a = Dog("Rex", 5)
b = a # aliasing -- a and b point to the same Dog
null (ဘာ object ကိုမှ မညွှန်းတဲ့ တန်ဖိုး)
“Object မရှိဘူး” လို့ ဆိုလိုတဲ့ အထူး တန်ဖိုးပါ။ Object တစ်ခု assign မလုပ်ရသေးတဲ့ reference variable (object ရဲ့ reference ကို သိမ်းတဲ့ variable) က null ကို ကိုင်ထားပါတယ်။ null ပေါ်မှာ method ခေါ်ရင် NullPointerException (null reference ကို သုံးမိလို့ ဖြစ်တဲ့ error) ကို throw (exception ဖြစ်ကြောင်း အချက်ပြ) လုပ်ပါတယ်။
Dog c = null;
c.bark(); // NullPointerException at runtime
c = None # None is Python's null
c.bark() # AttributeError at runtime - Python's NullPointerException moment
== နဲ့ .equals()
== က reference တွေကို နှိုင်းယှဉ်ပါတယ် (variable နှစ်ခုက object တစ်ခုတည်းကို ညွှန်းနေလား)။ .equals() က content (object ထဲက တန်ဖိုး) ကို နှိုင်းယှဉ်ပါတယ် (object နှစ်ခုက အဓိပ္ပာယ်အရ တူညီလား)။
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2); // false -- different objects
System.out.println(s1.equals(s2)); // true -- same content
s1 = "hello"
s2 = "hello"
print(s1 == s2) # True -- Python's == compares CONTENT, so it replaces .equals()
# Java's reference comparison (==) is Python's "is" operator - rarely needed
စာမေးပွဲ စည်းမျဉ်း: Java မှာ String (စာသား) တွေကို နှိုင်းယှဉ်ဖို့ .equals() ကို အမြဲ သုံးပါ။ == ကို သုံးရင် reference တွေကို နှိုင်းယှဉ်တာ ဖြစ်ပြီး အဲဒါက လိုချင်တာ မဟုတ်သလောက်ပါပဲ။ (Python မှာတော့ ပြောင်းပြန်ပါ: == က character (စာလုံး ဒါမှမဟုတ် သင်္ကေတ) တွေကိုယ်တိုင်ကို နှိုင်းယှဉ်ပြီး Python string တွေကို နှိုင်းယှဉ်ဖို့ မှန်ကန်တဲ့ နည်းလမ်းက == ပါပဲ။)
UML Class Diagram များ (B3.1.2)
UML Class Diagram (class တစ်ခုကို အကွက် သုံးကွက်နဲ့ ပြတဲ့ ပုံ)
Class တစ်ခုကို အပိုင်း သုံးပိုင်းနဲ့ ပြတဲ့ စံသတ်မှတ်ထားတဲ့ ပုံ ဖော်ပြချက်ပါ:
┌──────────────────────┐
│ ClassName │ ← class name
├──────────────────────┤
│ - name: String │ ← attributes (with visibility)
│ - age: int │
├──────────────────────┤
│ + getName(): String │ ← methods (with visibility)
│ + setAge(int): void │
└──────────────────────┘
ပုံထဲက ← နောက်က စာတွေက အကွက် တစ်ခုစီမှာ ဘာရေးလဲ ပြပါတယ်: class name (class ရဲ့ နာမည်)၊ attributes (with visibility) (attribute တွေ၊ visibility နဲ့အတူ)၊ methods (with visibility) (method တွေ၊ visibility နဲ့အတူ)။ Visibility (member ကို ဘယ်ကနေ မြင်ပြီး သုံးခွင့်ရလဲ ဆိုတာ) ကို အောက်က သင်္ကေတတွေနဲ့ ပြပါတယ်။
Visibility သင်္ကေတများ:
-= private+= public#= protected
မှတ်ထားရမယ့် အဓိပ္ပာယ်: “UML class diagram က class တစ်ခုကို အကွက် သုံးကွက်နဲ့ ပြပါတယ်: class နာမည်၊ attribute တွေ (type နဲ့ visibility ပါ) နဲ့ method တွေ (parameter၊ return type နဲ့ visibility ပါ)။”
Aggregation (B3.2.4)
Aggregation (သီးခြား ရှိနေနိုင်တဲ့ has-a ဆက်နွယ်မှု)
Object တစ်ခုက သူ့အပြင်မှာ ဖန်တီးထားတဲ့ နောက် object တစ်ခုရဲ့ reference ကို သိမ်းထားတဲ့ “has-a” (object တစ်ခုက နောက်တစ်ခုကို attribute အဖြစ် ထားတယ်) ဆက်နွယ်မှုပါ။ အဲဒီ အစိတ်အပိုင်းကို တခြား object တွေကလည်း reference လုပ်ထားနိုင်ပြီး သူ့ကို ကိုင်ထားခဲ့တဲ့ object မရှိတော့တဲ့ နောက်မှာလည်း ဆက်ရှိနေပါတယ်။
public class Kennel {
private Dog[] dogs; // Kennel HAS dogs, but dogs exist without the kennel
}
class Kennel():
def __init__(self):
self._dogs = [] # Kennel HAS dogs, but dogs exist without the kennel
Delegation (အလုပ်ကို တခြား object ဆီ လွှဲပေးခြင်း)
Object တစ်ခုက အလုပ်တစ်ခုရဲ့ တာဝန်ကို သူ့ထဲမှာ ပါတဲ့ object တစ်ခုဆီ လွှဲပေးတာပါ။ အဲဒီ object ရဲ့ method ကို ခေါ်ပြီး လွှဲပေးပါတယ်။
public String getDogName(int index) {
return dogs[index].getName(); // delegates to Dog's getName()
}
def get_dog_name(self, index):
return self._dogs[index].get_name() # delegates to Dog's get_name()
Inheritance (B3.2.1): HL
အောက်က ဝေါဟာရတွေက HL အတွက်ပဲ ဖြစ်ပါတယ်။ SL (Standard Level) နဲ့ HL (Higher Level) ဆိုတာ IB Computer Science သင်တန်းရဲ့ အဆင့်နှစ်ဆင့်ဖြစ်ပြီး HL အပိုင်းတွေက ပိုနက်နက်ရှိုင်းရှိုင်း လေ့လာရတဲ့ အပိုင်းတွေပါ။
Inheritance (parent class ကနေ အမွေဆက်ခံခြင်း)
Child class (subclass) တစ်ခုက parent class (superclass) တစ်ခုရဲ့ private မဟုတ်တဲ့ attribute နဲ့ method အားလုံးကို အလိုအလျောက် ရယူပြီး အသစ်တွေ ထပ်ထည့်နိုင်၊ ရှိပြီးသားတွေကို override (parent ရဲ့ method ကို child က အစားထိုး ရေးခြင်း) လုပ်နိုင်တဲ့ နည်းလမ်းပါ။ ဒါက code reuse (ရေးပြီးသား code ကို ပြန်သုံးခြင်း) ကို အားပေးပြီး “is-a” (class တစ်ခုက နောက်တစ်ခုကို extends လုပ်တယ်) ဆက်နွယ်မှုကို ကိုယ်စားပြုပါတယ်။
public class Puppy extends Dog {
private boolean trained;
public Puppy(String name, int age, boolean trained) {
super(name, age); // call parent constructor
this.trained = trained;
}
}
class Puppy(Dog): # (Dog) in the header is Python's "extends"
def __init__(self, name, age, trained):
super().__init__(name, age) # call the parent constructor
self._trained = trained
မှတ်ထားရမယ့် အဓိပ္ပာယ်: “Inheritance က subclass တစ်ခုကို superclass တစ်ခုရဲ့ attribute တွေနဲ့ method တွေကို ပြန်သုံး (inherit) ခွင့် ပေးပါတယ်။ ဒါက code ကို ပြန်သုံးနိုင်မှု (code reusability) ကို အားပေးပြီး is-a ဆက်နွယ်မှုကို တည်ဆောက်ပေးပါတယ်။”
Superclass (အမွေပေးတဲ့ parent class)
Inheritance hierarchy (class တွေ အဆင့်ဆင့် အမွေဆက်ခံထားတဲ့ ဖွဲ့စည်းပုံ) ထဲက parent class ပါ: သူ့ attribute တွေနဲ့ method တွေကို အောက်ကို လက်ဆင့်ကမ်းပေးတဲ့ class ပါ။ Parent class ဒါမှမဟုတ် base class (အခြေခံ class) လို့လည်း ခေါ်ပါတယ်။ class Puppy extends Dog မှာ Dog က superclass ပါ။
Subclass (အမွေဆက်ခံတဲ့ child class)
Superclass ကို extend လုပ်တဲ့ child class ပါ။ Superclass ရဲ့ ယူသုံးခွင့်ရှိတဲ့ member တွေကို inherit လုပ်ပြီး behaviour (object က လုပ်နိုင်တာတွေ) ကို ထပ်ထည့်တာ ဒါမှမဟုတ် override လုပ်တာ လုပ်ပါတယ်။ Subclass object တိုင်းက superclass object တစ်ခုလည်း ဖြစ် (is-a) တာကြောင့် Dog variable တစ်ခုက Puppy တစ်ခုကို ကိုင်ထားနိုင်ပါတယ်။
မှတ်ထားရမယ့် အဓိပ္ပာယ်: “Superclass ဆိုတာ member တွေကို အမွေပေးတဲ့ ပိုယေဘုယျကျတဲ့ class ပါ၊ subclass ဆိုတာ superclass ကို extend လုပ်တဲ့ ပိုသီးသန့်ကျတဲ့ class ပါ။”
extends (inheritance ကို ကြေညာတဲ့ keyword)
Inheritance ကို ကြေညာတဲ့ Java keyword ပါ။ class Child extends Parent ဆိုတာ Child က Parent ကနေ inherit လုပ်တယ်လို့ ဆိုလိုပါတယ်။ (Python မှာ extends keyword မရှိပါဘူး: class Child(Parent): က အလုပ် တစ်ခုတည်းကို လုပ်ပါတယ်။)
super (parent class ကို ညွှန်းတဲ့ keyword)
Parent class ကို ညွှန်းတဲ့ reference ပါ။ ဒါတွေအတွက် သုံးပါတယ်:
- Parent constructor ကို ခေါ်ဖို့:
super(args) - Override လုပ်ထားတဲ့ parent method ကို ခေါ်ဖို့:
super.methodName()
Constructor Chaining (constructor တစ်ခုက နောက် constructor တစ်ခုကို ဆက်ခေါ်ခြင်း)
Child constructor က ကိုယ်ပိုင် attribute တွေကို initialise (ပထမဆုံး တန်ဖိုး ထည့်) မလုပ်ခင် inherit လုပ်ထားတဲ့ attribute တွေကို initialise လုပ်ဖို့ super() နဲ့ parent constructor ကို ခေါ်တာပါ။
public Puppy(String name, int age, boolean trained) {
super(name, age); // parent constructor runs first
this.trained = trained; // then child-specific initialisation
}
def __init__(self, name, age, trained):
super().__init__(name, age) # parent constructor runs first
self._trained = trained # then child-specific initialisation
Polymorphism (B3.2.2): HL
Polymorphism (ပုံစံ အမျိုးမျိုး ယူနိုင်ခြင်း)
Parent type variable တစ်ခုက မတူတဲ့ child type တွေရဲ့ object တွေကို ကိုင်ထားနိုင်ပြီး method call (method ကို ခေါ်သုံးခြင်း) တွေက program run နေစဉ်မှာ မှန်ကန်တဲ့ child version ဆီ ရောက်သွား (dispatch) နိုင်စွမ်းပါ။
Dog[] dogs = new Dog[2];
dogs[0] = new Dog("Rex", 5);
dogs[1] = new Puppy("Spot", 1, false); // Puppy IS-A Dog
dogs = []
dogs.append(Dog("Rex", 5))
dogs.append(Puppy("Spot", 1, False)) # Puppy IS-A Dog - lists mix types freely
မှတ်ထားရမယ့် အဓိပ္ပာယ်: “Polymorphism က မတူတဲ့ subclass တွေရဲ့ object တွေကို ဘုံ superclass တစ်ခုရဲ့ object တွေလို ကိုင်တွယ်ခွင့် ပေးပါတယ်။ Method call တွေကိုတော့ program run နေစဉ်မှာ သင့်တော်တဲ့ subclass version အဖြစ် ဆုံးဖြတ်ပေးပါတယ်။”
Method Overriding (parent ရဲ့ method ကို child က အစားထိုး ရေးခြင်း)
Child class က parent ကနေ inherit လုပ်ထားတဲ့ method တစ်ခုအတွက် ကိုယ်ပိုင် implementation (တကယ် အလုပ်လုပ်အောင် ရေးထားတဲ့ code) ကို ပေးတာပါ။ Child type object တွေအတွက် child version က parent version ကို အစားထိုးပါတယ်။
public class Puppy extends Dog {
@Override
public void bark() {
System.out.println(getName() + " says Yap!"); // replaces parent's bark()
}
}
class Puppy(Dog):
def bark(self): # same name = override (Python has no @Override annotation)
print(self.get_name(), "says Yap!") # replaces parent's bark()
@Override Annotation (compiler ကို ရည်ရွယ်ချက် ပြောပြတဲ့ အမှတ်အသား)
Compiler (code တစ်ခုလုံးကို ကြိုတင် ဘာသာပြန်တဲ့ program) ကို “parent method တစ်ခုကို override လုပ်ဖို့ ရည်ရွယ်ပါတယ်” လို့ ပြောဖို့ method ရဲ့ ရှေ့မှာ ထည့်တဲ့ အမှတ်အသားပါ။ Method signature (method ရဲ့ နာမည်နဲ့ parameter type တွေ) က ဘယ် parent method နဲ့မှ မကိုက်ရင် compiler က error ပြပါတယ်: ဒါကြောင့် စာလုံးပေါင်း အမှားတွေကို စောစော ဖမ်းမိပါတယ်။ (ဒါက Java မှာပဲ ရှိပါတယ်: Python မှာတော့ child class ထဲမှာ method ကို ပြန်ရေးလိုက်ရုံပါပဲ၊ စာလုံးပေါင်း အမှားကို ဖမ်းပေးမယ့် annotation မရှိပါဘူး။)
Dynamic Dispatch (Runtime Polymorphism: program run နေစဉ် ဘယ် method ကို run မလဲ ဆုံးဖြတ်ခြင်း)
Override လုပ်ထားတဲ့ method ရဲ့ ဘယ် version ကို run မလဲဆိုတာကို Java က compile time (compile လုပ်ချိန်) မှာ မဟုတ်ဘဲ program run နေစဉ် မှာ ဆုံးဖြတ်တဲ့ လုပ်ငန်းစဉ်ပါ။ Variable type ပေါ်မှာ မဟုတ်ဘဲ တကယ့် object type ပေါ်မှာ မူတည်ပြီး ဆုံးဖြတ်ပါတယ်။
Dog d = new Puppy("Spot", 1, false);
d.bark(); // calls Puppy's bark(), not Dog's -- decided at runtime
d = Puppy("Spot", 1, False)
d.bark() # calls Puppy's bark(), not Dog's -- the actual object decides at runtime
Overriding နဲ့ Overloading
Overriding ကို overloading (နာမည်တူပြီး parameter မတူတဲ့ method များ) နဲ့ နှိုင်းယှဉ်ထားပါတယ်:
| Overriding | Overloading | |
|---|---|---|
| ဘယ်မှာ | Child class | Class တစ်ခုတည်းထဲမှာ |
| Method နာမည် | တူ | တူ |
| Parameter | တူ | မတူ |
| ဆုံးဖြတ်ချိန် | Program run နေစဉ် | Compile time |
| Keyword | @Override | မလိုပါ |
Abstraction (B3.2.3): HL
Abstract Class (object တိုက်ရိုက် မဖန်တီးနိုင်တဲ့ class)
abstract keyword နဲ့ ကြေညာထားပြီး instantiate လုပ်လို့ မရတဲ့ (object ဖန်တီးလို့ မရတဲ့) class ပါ။ ဘုံ ဖွဲ့စည်းပုံကို သတ်မှတ်ပေးတဲ့ base class အဖြစ် အလုပ်လုပ်ပြီး subclass တွေက implement (တကယ် အလုပ်လုပ်အောင် ရေး) လုပ်ရမယ့် abstract method တွေ ပါနိုင်ပါတယ်။
public abstract class Animal {
private String name;
public Animal(String name) { this.name = name; }
public String getName() { return name; }
public abstract void makeSound(); // no body -- subclasses must provide one
}
# Python has no abstract keyword - raising an error if a child forgets
# to override is the closest simple equivalent
class Animal():
def __init__(self, name):
self._name = name
def get_name(self):
return self._name
def make_sound(self):
raise NotImplementedError # "abstract" - subclasses must provide a real body
Abstract Method (body မပါဘဲ ကြေညာချက်ပဲ ပါတဲ့ method)
Body (method ထဲမှာ ရေးထားတဲ့ code) မပါဘဲ ({} မပါဘဲ) ကြေညာထားတဲ့ method ပါ။ Concrete (abstract မဟုတ်တဲ့ ပုံမှန်) subclass တိုင်းက implementation တစ်ခု ပေးပြီး အဲဒီ method ကို မဖြစ်မနေ override လုပ်ရပါတယ်။
public abstract void makeSound(); // declaration only -- no body
def make_sound(self):
raise NotImplementedError # the only "body" is an error - children must override
မှတ်ထားရမယ့် အဓိပ္ပာယ်: “Abstract class ကို instantiate လုပ်လို့ မရဘဲ abstract method တွေ ပါနိုင်ပါတယ်: abstract method ဆိုတာ implementation မပါဘဲ concrete subclass တိုင်းက override လုပ်ရမယ့် method တွေပါ။”
Interface (class တစ်ခု implement လုပ်ရမယ့် method စာရင်း)
Class တစ်ခုက မဖြစ်မနေ implement လုပ်ရမယ့် method တွေကို သတ်မှတ်ပေးပြီး ကိုယ်တိုင်က implementation ဘာမှ မပေးတဲ့ contract (လိုက်နာရမယ့် သဘောတူချက်) ပါ။ Class တစ်ခုက interface အများကြီးကို implement လုပ်နိုင်ပါတယ် (parent class တစ်ခုတည်းကပဲ inherit လုပ်ခွင့်ရှိတဲ့ single inheritance နဲ့ မတူပါဘူး)။
public interface Trainable {
void train(); // all methods are implicitly public and abstract
boolean isTrained();
}
public class Puppy extends Dog implements Trainable {
public void train() { /* implementation */ }
public boolean isTrained() { return trained; }
}
# Python has no interface keyword - a stub class is the closest simple equivalent;
# Python relies on convention (duck typing) where Java enforces the contract
class Trainable():
def train(self):
raise NotImplementedError
def is_trained(self):
raise NotImplementedError
class Puppy(Dog, Trainable): # inherit the parent AND the contract class together
def train(self):
pass # implementation goes here
def is_trained(self):
return self._trained
Interface နဲ့ Abstract Class:
| Interface | Abstract Class | |
|---|---|---|
| Instantiate လုပ်လို့ ရလား | မရ | မရ |
| အများကြီး ရလား | Class တစ်ခုက interface အများကြီးကို implement လုပ်နိုင် | Class တစ်ခုက abstract class တစ်ခုတည်းကိုပဲ extend လုပ်နိုင် |
| Method များ | အားလုံး abstract (body မပါ) | Abstract နဲ့ concrete ရောပါနိုင် |
| Field များ | Constant (ပြောင်းလို့ မရတဲ့ တန်ဖိုး) တွေပဲ (static final) | Instance variable တွေ ထားလို့ ရ |
| ဘယ်အချိန် သုံးမလဲ | လုပ်နိုင်စွမ်း တစ်ခုကို သတ်မှတ်ဖို့ (“can do”: လုပ်နိုင်တယ်) | ဘုံ base class တစ်ခုကို သတ်မှတ်ဖို့ (“is a kind of”: အမျိုးအစား တစ်မျိုး ဖြစ်တယ်) |
Composition နဲ့ Aggregation (B3.2.4): HL
Composition (ပိုခိုင်မာတဲ့ has-a ဆက်နွယ်မှု)
ပိုခိုင်မာတဲ့ “has-a” ဆက်နွယ်မှုပါ: တစ်ခုလုံး (whole) က သူ့ အစိတ်အပိုင်း (part) တွေကို ကိုယ်တိုင် ဖန်တီးပြီး ပိုင်ဆိုင်ပါတယ်။ အစိတ်အပိုင်း တစ်ခုစီကို တစ်ခုလုံးရဲ့ အထဲမှာ (အများအားဖြင့် သူ့ constructor ထဲမှာ) ဖန်တီးပြီး တခြား ဘယ် object ကိုမှ အဲဒီ အစိတ်အပိုင်းရဲ့ reference ကို မပေးပါဘူး။ အစိတ်အပိုင်းက သူ့ဘာသာ သီးသန့် အသုံးမဝင်တာကြောင့် အစိတ်အပိုင်းတွေက တစ်ခုလုံး ရှိနေသရွေ့ပဲ ရှိပါတယ်။
public class Board {
private Square[] squares;
public Board(int numSquares) {
squares = new Square[numSquares];
for (int i = 0; i < numSquares; i++) {
squares[i] = new Square(i + 1); // each square is made BY the board
}
}
// no other object holds these squares, so they go when the Board goes
}
class Board():
def __init__(self, num_squares):
self._squares = []
for i in range(num_squares):
self._squares.append(Square(i + 1)) # each square is made BY the board
# no other object holds these squares, so they go when the Board goes
Aggregation (ပြန်ကြည့်ခြင်း: HL အမြင်)
ပိုအားနည်းတဲ့ “has-a” ဆက်နွယ်မှုပါ။ Container (တခြား object တွေကို သိမ်းထားတဲ့ object) က သူ့အပြင်မှာ ဖန်တီးထားတဲ့ object တစ်ခုရဲ့ reference ကို လက်ခံရရှိပြီး အဲဒီ object ကို မပိုင်ဘဲ reference ကိုပဲ ကိုင်ထားပါတယ်။ တခြား code က အဲဒီ object တစ်ခုတည်းကို ကိုင်ထားနိုင်တယ်၊ နောက် container တစ်ခုဆီ ရွှေ့နိုင်တယ်၊ ဒါမှမဟုတ် container မရှိတော့တဲ့ နောက်မှာလည်း ဆက်သုံးနိုင်ပါတယ်။
public class Kennel {
private Dog resident;
public Kennel(Dog dog) {
this.resident = dog; // the dog was made outside, so it exists on its own
}
}
class Kennel():
def __init__(self, dog):
self._resident = dog # the dog was made outside, so it exists on its own
မှတ်ထားရမယ့် အဓိပ္ပာယ် (composition နဲ့ aggregation): “Composition မှာ တစ်ခုလုံးက သူ့ အစိတ်အပိုင်းတွေကို ဖန်တီးပြီး အဲဒီ အစိတ်အပိုင်းတွေကို ကိုင်ထားတဲ့ တစ်ခုတည်းသော object ဖြစ်တာကြောင့် အစိတ်အပိုင်းတွေက တစ်ခုလုံး ရှိနေသရွေ့ပဲ ရှိပါတယ် (lifecycle dependency: ရှိနေတဲ့ ကာလကို မှီခိုခြင်း)။ Aggregation မှာတော့ တစ်ခုလုံးက တခြားနေရာမှာ ဖန်တီးထားတဲ့ object တွေရဲ့ reference တွေကို ကိုင်ထားတာကြောင့် အဲဒီ object တွေကို မျှသုံးနိုင်ပြီး တစ်ခုလုံး မရှိတော့တဲ့ နောက်မှာလည်း ဆက်ရှိနေနိုင်ပါတယ်။”
Composition၊ Aggregation နဲ့ Inheritance နှိုင်းယှဉ်ချက်
| ဆက်နွယ်မှု | အမျိုးအစား | ဥပမာ | Coupling (class တွေ အချင်းချင်း မှီခိုမှု) |
|---|---|---|---|
| Inheritance | “is-a” | Puppy is a Dog (Puppy က Dog တစ်မျိုး ဖြစ်တယ်) | Tight (မှီခိုမှု များ) |
| Composition | “has-a” (ခိုင်မာ) | Board has Squares (Board မှာ Square တွေ ရှိတယ်) | Medium (အလယ်အလတ်) |
| Aggregation | “has-a” (အားနည်း) | Kennel has a Dog (Kennel မှာ Dog တစ်ကောင် ရှိတယ်) | Loose (မှီခိုမှု နည်း) |
ဒီဇိုင်း စည်းမျဉ်း: ဖြစ်နိုင်ရင် inheritance ထက် composition ကို ရွေးပါ။ Composition က tight coupling (class တွေ အချင်းချင်း အလွန် မှီခိုနေခြင်း) မရှိဘဲ ပြောင်းလွယ်ပြင်လွယ် ရှိစေပါတယ်။
Design Pattern များ (B3.2.5): HL
Singleton Pattern (instance တစ်ခုတည်းပဲ ရှိတဲ့ class ပုံစံ)
Singleton pattern က class တစ်ခုမှာ instance တစ်ခုတည်းပဲ ရှိအောင် သေချာစေပြီး အဲဒီ instance ကို ယူသုံးဖို့ program ရဲ့ ဘယ်နေရာကမဆို သုံးလို့ရတဲ့ global point of access (ဘုံ ယူသုံးရာ နေရာ တစ်ခု) ကို ပေးပါတယ်။ Database connection တစ်ခု၊ settings manager (program ရဲ့ setting တွေကို စီမံတဲ့ object) တစ်ခုလို မျှသုံးရတဲ့ resource တွေအတွက် အသုံးဝင်ပါတယ်။
Factory Pattern (ဘယ် object ဖန်တီးမလဲ ဆုံးဖြတ်ပေးတဲ့ ပုံစံ)
Factory pattern က object ဖန်တီးပုံ logic ကို caller (ခေါ်တဲ့ method) ကို မပြဘဲ object တွေကို ဖန်တီးဖို့ method တစ်ခုကို သုံးပါတယ်။ Caller က ဘယ်လို object အမျိုးအစား လိုချင်လဲ ပြောပြီး factory က သင့်တော်တဲ့ subclass instance ကို ပြန်ပေးပါတယ်။
Observer Pattern (state ပြောင်းရင် အလိုအလျောက် အသိပေးတဲ့ ပုံစံ)
Observer pattern က one-to-many dependency (object တစ်ခုကို object အများက မှီခိုတဲ့ ဆက်နွယ်မှု) ကို သတ်မှတ်ပါတယ်: object တစ်ခု (subject) ရဲ့ state ပြောင်းသွားတဲ့အခါ သူ့ကို မှီခိုနေတဲ့ object အားလုံး (observer တွေ) ကို အလိုအလျောက် အသိပေးပြီး update လုပ်ပေးပါတယ်။ Event-driven system (ဖြစ်ရပ်တွေကို တုံ့ပြန်ပြီး အလုပ်လုပ်တဲ့ စနစ်) တွေအတွက် အသုံးဝင်ပါတယ်။
စာမေးပွဲမှာ ဒီ pattern တွေကို အပြည့်အစုံ implement လုပ်ပြီး ရေးဖို့ မမျှော်လင့်ပါဘူး။ ဒါပေမဲ့ pattern တစ်ခုစီက ဘာလုပ်လဲ၊ ဘယ်အချိန်မှာ အသုံးဝင်လဲ ဆိုတာကိုတော့ ရှင်းပြနိုင်ရပါမယ်။
အမြန် ကြည့်ရန် ဇယား
| ဝေါဟာရ | တစ်ကြောင်းတည်း အဓိပ္ပာယ် | အပိုင်း |
|---|---|---|
| Class | Attribute တွေနဲ့ method တွေကို သတ်မှတ်ပေးတဲ့ blueprint | B3.1 |
| Object | ကိုယ်ပိုင် ဒေတာ ရှိတဲ့ class တစ်ခုရဲ့ instance | B3.1 |
| Instantiation | new နဲ့ object ဖန်တီးခြင်း | B3.1 |
| Constructor | Object အသစ်ကို initialise လုပ်ပေးတဲ့ method | B3.1 |
| Encapsulation | ဒေတာ + method ကို စုထုပ်ပြီး ယူသုံးခွင့် ကန့်သတ်ခြင်း | B3.1.5 |
| Information Hiding | အတွင်းပိုင်း state ကို အပြင်က code မမြင်ရအောင် လုပ်ခြင်း | B3.1.5 |
| Access Modifier | Member ကို ဘယ်ကနေ မြင်ခွင့်ရလဲ ထိန်းချုပ်တဲ့ keyword | B3.1.5 |
| Getter / Setter | Private attribute တွေကို ဖတ်တဲ့ / ရေးတဲ့ method များ | B3.1.5 |
this | လက်ရှိ object ကို ညွှန်းတဲ့ reference | B3.1 |
| Static variable | Class ရဲ့ object အားလုံး မျှသုံးတယ် | B3.1.3 |
| Static method | Instance မဟုတ်ဘဲ class က ပိုင်တယ် | B3.1.3 |
| Reference | Object ကို ညွှန်းတဲ့ memory address | B3.1 |
| Aliasing | Variable နှစ်ခုက object တစ်ခုတည်းကို ညွှန်းခြင်း | B3.1 |
null | “Object မရှိဘူး” လို့ ဆိုလိုတဲ့ အထူး တန်ဖိုး | B3.1 |
| UML class diagram | Class တစ်ခုကို အပိုင်း သုံးပိုင်းနဲ့ ပြတဲ့ ပုံ | B3.1.2 |
| Aggregation | အားနည်းတဲ့ has-a (ရှိနေတဲ့ ကာလ သီးခြားစီ) | B3.2.4 |
| Delegation | ပါဝင်နေတဲ့ object ရဲ့ method ဆီ အလုပ် လွှဲပေးခြင်း | B3.1 |
| Inheritance | Subclass က superclass ရဲ့ member တွေကို ပြန်သုံးတယ် (is-a) | B3.2.1 HL |
| Superclass | Member တွေကို အမွေပေးတဲ့ parent class | B3.2.1 HL |
| Subclass | Superclass ကို extend လုပ်တဲ့ child class | B3.2.1 HL |
| Polymorphism | Parent variable က child object တွေကို ကိုင်ထားတယ်၊ run နေစဉ် dispatch လုပ်တယ် | B3.2.2 HL |
| Method Overriding | Child က parent method ကို အစားထိုးတယ် (signature တူ) | B3.2.2 HL |
| Dynamic Dispatch | Override လုပ်ထားတဲ့ method ကို run နေစဉ် ရွေးခြင်း | B3.2.2 HL |
| Abstract Class | Instantiate လုပ်လို့ မရ၊ abstract method တွေ ပါနိုင် | B3.2.3 HL |
| Interface | Class တစ်ခု implement လုပ်ရမယ့် method တွေရဲ့ contract | B3.2.3 HL |
| Composition | ခိုင်မာတဲ့ has-a (lifecycle dependency) | B3.2.4 HL |
| Design Pattern | နာမည်ပေးထားပြီး ပြန်သုံးလို့ရတဲ့ ဖြေရှင်းပုံ template (singleton, factory, observer) | B3.2.5 HL |
Quick Check (အမြန် စစ်ဆေးမေးခွန်း)
Q1. "ဒေတာနဲ့ method တွေကို unit တစ်ခုတည်းထဲ စုထုပ်ပြီး အတွင်းပိုင်း ဒေတာကို တိုက်ရိုက် ယူသုံးတာကို ကန့်သတ်ခြင်း" ကို ဘယ် ဝေါဟာရက ဖော်ပြသလဲ။
Q2. Static method တစ်ခုက instance variable တွေကို (object reference မပါဘဲ) တိုက်ရိုက် ယူသုံးလို့ ဘာကြောင့် မရတာလဲ။
Q3. String s1 = new String("hi"); နဲ့ String s2 = new String("hi"); ကို ပေးထားရင် s1 == s2 က ဘာကို return ပြန်သလဲ။
Q4. Composition နဲ့ aggregation ကြားက အဓိက ကွာခြားချက်က ဘာလဲ။
Q5. Child class တစ်ခုက parent method တစ်ခုနဲ့ နာမည်တူ၊ parameter တူတဲ့ method တစ်ခုကို သတ်မှတ်ပြီး parent method ရဲ့ behaviour ကို အစားထိုးပါတယ်။ ဒါကို ဘယ်လို ခေါ်သလဲ:
Fill in the Blanks (ကွက်လပ် ဖြည့်ခြင်း)
ဒီ အဓိပ္ပာယ်တွေထဲက ပျောက်နေတဲ့ ဝေါဟာရတွေကို ဖြည့်ပါ။ အဓိပ္ပာယ်တွေကို English လို ရေးထားပြီး အဖြေကိုလည်း English စကားလုံး (စာလုံး အသေး) နဲ့ ရိုက်ထည့်ပါ။ စာကြောင်း တစ်ကြောင်းစီရဲ့ မြန်မာ အဓိပ္ပာယ်ကို box ရဲ့ အောက်မှာ ပေးထားပါတယ်။
ကွက်လပ် ဖြည့်ပါ: OOP အဓိပ္ပာယ် တစ်ခုစီကို မှန်ကန်တဲ့ ဝေါဟာရနဲ့ ပြည့်စုံအောင် ဖြည့်ပါ။
// 1. A __________ is a blueprint that defines attributes and methods.
// Objects are instances of a .
// 2. Making attributes private and providing public getters/setters
// is an example of .
// 3. When two variables point to the same object in memory,
// this is called .
// 4. A variable declared as static is shared by all
// of the class.
// 5. The process of creating an object using new is
// called . စာကြောင်းတွေရဲ့ မြန်မာ အဓိပ္ပာယ်:
- __ ဆိုတာ attribute တွေနဲ့ method တွေကို သတ်မှတ်ပေးတဲ့ blueprint ပါ။ Object တွေက __ တစ်ခုရဲ့ instance တွေပါ။
- Attribute တွေကို private လုပ်ပြီး public getter/setter တွေ ပေးတာက __ ရဲ့ ဥပမာ တစ်ခုပါ။
- Variable နှစ်ခုက memory ထဲက object တစ်ခုတည်းကို ညွှန်းနေရင် ဒါကို __ လို့ ခေါ်ပါတယ်။
- Static လို့ ကြေညာထားတဲ့ variable ကို class ရဲ့ __ အားလုံးက မျှသုံးပါတယ်။
newကို သုံးပြီး object ဖန်တီးတဲ့ လုပ်ငန်းစဉ်ကို __ လို့ ခေါ်ပါတယ်။
Spot the Error (အမှား ရှာခြင်း)
ကျောင်းသား တစ်ယောက်က test တစ်ခုမှာ ဒီ အဓိပ္ပာယ်တွေကို ရေးခဲ့ပါတယ်။ မမှန်တဲ့ ပြောဆိုချက် ပါတဲ့ line ကို ရှာပါ။
Bug Hunt (အမှား ရှာပါ): ဒီ comment တွေထဲက တစ်ခုမှာ OOP အကြောင်း မမှန်တဲ့ ပြောဆိုချက် ပါပါတယ်။ အဲဒီ line ကို ရှာပါ။
Line တွေရဲ့ မြန်မာ အဓိပ္ပာယ်: (1) Class ဆိုတာ attribute တွေနဲ့ method တွေကို သတ်မှတ်ပေးတဲ့ blueprint ပါ။ (2) Object ဆိုတာ new နဲ့ ဖန်တီးထားတဲ့ class တစ်ခုရဲ့ instance ပါ။ (3) Encapsulation က ဒေတာနဲ့ method တွေကို စုထုပ်ပြီး ယူသုံးခွင့်ကို ကန့်သတ်ပါတယ်။ (4) Static method က static variable တွေရော instance variable တွေကိုပါ ယူသုံးနိုင်ပါတယ်။ (5) Aggregation က object တွေကြားက has-a ဆက်နွယ်မှုကို ကိုယ်စားပြုပါတယ်။
Line 4 အတွက် မှန်ကန်တဲ့ ပြင်နည်းကို ရွေးပါ:
Predict the Output (output ကို ကြိုခန့်မှန်းခြင်း)
ကြိုခန့်မှန်းပါ: ဒီ code က ဘာကို print ထုတ်မလဲ။ (ဒီမေးခွန်းက polymorphism နဲ့ dynamic dispatch ကို နားလည်မှု ရှိမရှိ စစ်ပါတယ်။)
public class Dog {
public void speak() { System.out.println("Woof!"); }
}
public class Puppy extends Dog {
@Override
public void speak() { System.out.println("Yap!"); }
}
// In main:
Dog d = new Puppy();
d.speak(); ကြိုခန့်မှန်းပါ: ဒီ code က ဘာကို print ထုတ်မလဲ။ (ဒီမေးခွန်းက reference နဲ့ aliasing ကို နားလည်မှု ရှိမရှိ စစ်ပါတယ်။)
Dog a = new Dog("Rex", 5);
Dog b = a;
a = new Dog("Bella", 3);
System.out.println(b.getName()); Practice Exercises (လေ့ကျင့်ခန်းများ)
Core (အခြေခံ အဆင့်)
-
ဝေါဟာရ ငါးခုကို Define လုပ်ပါ: ဒီစာမျက်နှာကို မကြည့်ဘဲ class၊ object၊ encapsulation၊ constructor နဲ့ access modifier တို့ရဲ့ တိကျတဲ့ အဓိပ္ပာယ်တွေကို ရေးပါ။ ပြီးရင် အပေါ်က အဓိပ္ပာယ်တွေနဲ့ နှိုင်းယှဉ်ကြည့်ပါ။
-
Code ကနေ UML ဆွဲခြင်း: ဒီစာမျက်နှာမှာ ပြထားတဲ့
Dogclass အတွက် UML class diagram တစ်ခု ဆွဲပါ။ Attribute အားလုံး၊ constructor နဲ့ method အားလုံးကို မှန်ကန်တဲ့ visibility သင်္ကေတတွေနဲ့ ထည့်ပါ။ -
Static နဲ့ Instance:
getDogCount()method က ဘာကြောင့် static ဖြစ်သင့်ပြီးgetName()method က ဘာကြောင့် static မဖြစ်သင့်လဲဆိုတာ ဥပမာ တစ်ခုနဲ့ Explain လုပ်ပါ။
Extension (တိုးချဲ့ အဆင့်)
- ဆက်နွယ်မှုကို ခွဲခြားခြင်း: အတွဲ တစ်ခုစီအတွက် ဆက်နွယ်မှုက inheritance၊ composition ဒါမှမဟုတ် aggregation ဖြစ်လားဆိုတာ State (ဖော်ပြပါ) လုပ်ပြီး သင့်အဖြေကို Justify (ဘာကြောင့် ရွေးတာလဲ အကြောင်းပြချက်နဲ့ ပြပါ) လုပ်ပါ:
- Car နဲ့ Engine
- Student နဲ့ Person
- Library နဲ့ Book
- ArrayList နဲ့ Object
- Compare and contrast (တူတာနဲ့ ကွာတာ နှစ်ခုလုံးကို ဖော်ပြပါ): method overriding နဲ့ method overloading ကို နှိုင်းယှဉ်တဲ့ စာပိုဒ် တစ်ပိုဒ် ရေးပါ။ တစ်ခုစီ ဘယ်မှာ ဖြစ်လဲ၊ parameter တွေ ဘယ်လို ကွာလဲ၊ ဘယ် method ကို ခေါ်မလဲဆိုတာ Java က ဘယ်အချိန်မှာ ဆုံးဖြတ်လဲ ဆိုတာတွေ ထည့်ရေးပါ။
Challenge (စိန်ခေါ်မှု အဆင့်)
[2 marks] စတာတွေက အပိုင်း တစ်ခုချင်းစီအတွက် ရနိုင်တဲ့ အမှတ်ပါ။ IB စာမေးပွဲ မေးခွန်းတွေမှာ အမှတ်ကို ဒီလို ပြလေ့ ရှိပါတယ်။
- Design pattern အခြေအနေ:
- (a) Singleton design pattern ရဲ့ ရည်ရွယ်ချက်ကို Outline (အကျဉ်းချုပ် ဖော်ပြပါ) လုပ်ပါ။ [2 marks]
- (b) ကျောင်း စီမံခန့်ခွဲမှု system တစ်ခုမှာ
SchoolDatabaseobject တစ်ခုတည်းပဲ အမြဲ ရှိနေဖို့ လိုပါတယ်။ ဘယ် design pattern က သင့်တော်သလဲ။ အဲဒီ pattern ဘယ်လို အလုပ်လုပ်လဲ၊ database connection အများကြီး ဖန်တီးရင် ဘာကြောင့် ပြဿနာ ဖြစ်မလဲဆိုတာ Explain လုပ်ပါ။ [5 marks]
ဆက်စပ် ခေါင်းစဉ်များ
- ဒီစာမျက်နှာက အနှစ်ချုပ်ထားတဲ့ ဝေါဟာရတွေ ပါတဲ့ စာမျက်နှာများ: What is OOP (OOP ဆိုတာ ဘာလဲ)၊ Classes and Objects (class နဲ့ object များ)၊ Encapsulation၊ Access Modifiers၊ Static Members (static member များ)၊ Object References (object reference များ)
- HL ဝေါဟာရတွေ ပါတဲ့ စာမျက်နှာများ: Inheritance၊ Polymorphism၊ Abstract Classes၊ Interfaces၊ Composition vs Inheritance (composition နဲ့ inheritance နှိုင်းယှဉ်ချက်)
- ဒီ ဝေါဟာရတွေကို အသုံးချမယ့် စာမျက်နှာများ: OOP Method Patterns (OOP method ရေးပုံ ပုံစံများ)၊ Selection Sort on Objects (object တွေကို selection sort နဲ့ စီခြင်း)