ရိုးရိုးလေး ရှင်းပြမယ်
Polymorphism ကို ရိုးရိုးလေး ပြောရရင်
Polymorphism ဆိုတာ "ပုံစံ အများအပြား" လို့ အဓိပ္ပာယ်ရပါတယ်။ Code က parent type တစ်ခု (Animal) ကိုပဲ သိပြီး makeSound() ကို ခေါ်လိုက်ပေမဲ့ တကယ် run သွားတဲ့ code က object ရဲ့ အမှန်တကယ် class ပေါ် မူတည်ပါတယ်: Dog ဆိုရင် ဟောင်ပြီး Cat ဆိုရင် ညောင်ပါတယ်။ ခေါ်တဲ့ code က တစ်ကြောင်းတည်း ဖြစ်ပေမဲ့ ရလဒ်က object အလိုက် ကွဲပြားပါတယ်။
TV remote တစ်ခုကို စဉ်းစားကြည့်ပါ။ Power ခလုတ်ကို နှိပ်တာ အတူတူပါပဲ၊ ဒါပေမဲ့ ဘယ် brand TV ကို ချိန်ထားလဲ ပေါ်မူတည်ပြီး TV အတွင်းပိုင်းမှာ ဖြစ်တာက မတူပါဘူး။ သင်ကတော့ "power" ဆိုတဲ့ ညွှန်ကြားချက်ကိုပဲ သိဖို့ လိုပြီး ဘယ်လို အကောင်အထည်ဖော်မလဲဆိုတာ TV ကိုယ်တိုင် ဆုံးဖြတ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: type အသစ် ထပ်လာတိုင်း if-else ရှည်ရှည်
Polymorphism မသိခင် developer တွေက object ရဲ့ class ကို စစ်ပြီး ကိုယ်တိုင် ခွဲထုတ်လေ့ ရှိပါတယ်။ အောက်က method က တိရစ္ဆာန် နှစ်မျိုးအတွက် အလုပ်လုပ်ပါတယ်၊ ဒါပေမဲ့ Bird ထပ်ထည့်လိုက်တဲ့နေ့မှာ ဒီ method ကို ပြင်ဖို့ မေ့သွားရင် ငှက်တွေက "Unknown animal" လို့ပဲ ပြနေပါမယ်။ ဒီလို if-else chain တွေက codebase တစ်ခုလုံးမှာ ပြန့်ကျဲနေလေ့ ရှိလို့ type အသစ် တစ်ခုက နေရာ ဆယ်ခုလောက်ကို ပြင်ခိုင်းပါတယ်။
static void playSound(Animal animal) {
if (animal instanceof Dog) {
System.out.println("Woof!");
} else if (animal instanceof Cat) {
System.out.println("Meow!");
} else {
System.out.println("Unknown animal"); // every new Bird lands here
}
}Behavior ကို object ထဲ ထည့်ထားပါ
ပြဿနာက "ဘယ်လို အသံထွက်မလဲ" ဆိုတဲ့ အသိကို object ရဲ့ အပြင်မှာ ထားလိုက်တာပါ။ Polymorphism က အဲဒီ အသိကို class တစ်ခုချင်းစီထဲ ပြန်ပို့ပေးပါတယ်: Dog က ကိုယ့် makeSound() ကို ကိုယ်ရေးပြီး ခေါ်တဲ့ code က animal.makeSound() လို့ပဲ ပြောပါတယ်။ Bird အသစ် ထပ်လာရင် Bird class တစ်ခုတည်းကို ရေးရုံပါပဲ၊ ရှိပြီးသား code တစ်ကြောင်းမှ ပြင်စရာ မလိုပါဘူး။
Core concept: declared type နဲ့ runtime class
Animal pet = new Dog(); ဆိုတဲ့ line မှာ type နှစ်ခု ပါပါတယ်။ Animal က variable ရဲ့ declared type (compile-time type) ဖြစ်ပြီး Dog ကတော့ heap ပေါ်က object ရဲ့ runtime class ပါ။ Dog က Animal တစ်မျိုး ဖြစ်တဲ့အတွက် ဒီ assignment (upcast) က cast မလိုဘဲ အလိုအလျောက် အလုပ်လုပ်ပါတယ်။ Animal parameter ယူတဲ့ method ကို Dog ပို့တာ၊ Dog နဲ့ Cat တွေကို List<Animal> တစ်ခုထဲ အတူ သိမ်းတာတွေကလည်း upcast တွေပါပဲ။ Polymorphism တစ်ခုလုံးက ဒီ type နှစ်ခုကို ဘယ်သူက ဘာအတွက် သုံးလဲ ဆိုတာပေါ်မှာ တည်ပါတယ်။
Compiler က declared type ကိုပဲ ကြည့်ပါတယ်။ pet.makeSound() က Animal မှာ ရှိလို့ compile ဖြစ်ပေမဲ့ pet.fetch() ကတော့ Animal မှာ fetch() မရှိတဲ့အတွက် object က တကယ် Dog ဖြစ်နေလည်း compile error ပါ။ Program run တဲ့အခါကျမှ JVM က runtime class ကို ကြည့်ပြီး override လုပ်ထားတဲ့ version ကို ရွေးပါတယ်၊ ဒါကို dynamic dispatch ဒါမှမဟုတ် late binding လို့ ခေါ်ပါတယ်။ ဒါကြောင့် pet.makeSound() က Dog ရဲ့ version ကို run ပါတယ်။
Who decides what
----------------
Animal pet = new Dog();
| |
| +--> runtime class: Dog (JVM, while running)
+--> declared type: Animal (compiler, before running)
pet.makeSound()
compiler: does Animal have makeSound()? yes -> compiles
JVM: which class is the object? Dog -> runs Dog.makeSound()
pet.fetch()
compiler: does Animal have fetch()? no -> compile errorDynamic dispatch က override လုပ်ထားတဲ့ instance method တွေမှာပဲ အလုပ်လုပ်ပါတယ်။ Field တွေ၊ static method တွေနဲ့ private method တွေကို declared type နဲ့ပဲ ဆုံးဖြတ်ပါတယ်။ Overload (နာမည်တူ parameter မတူ) တွေထဲက ဘယ်ဟာကို ခေါ်မလဲဆိုတာကိုလည်း compiler က argument တွေရဲ့ declared type နဲ့ compile time မှာ ရွေးပြီးသား ဖြစ်ပါတယ်။ Object ရဲ့ subclass-only method ကို လိုအပ်ရင် instanceof pattern (if (pet instanceof Dog dog)) နဲ့ စစ်ပြီးမှ downcast လုပ်ပါ၊ မစစ်ဘဲ (Dog) pet လို့ cast လုပ်ရင် Cat ဖြစ်နေတဲ့အခါ ClassCastException ဖြစ်ပါတယ်။
Hierarchy အဆင့်များတဲ့အခါ JVM က runtime class ကနေ စပြီး အပေါ်ကို တက်ရှာကာ ပထမဆုံး တွေ့တဲ့ override ကို run ပါတယ်။ Puppy extends Dog ဖြစ်ပြီး Puppy က makeSound() ကို override မလုပ်ထားရင် Dog ရဲ့ version က run ပါတယ်။ ဒီ စနစ်ရဲ့ တကယ့် အကျိုးကျေးဇူးကတော့ Animal ကို လက်ခံတဲ့ method တစ်ခုက ဒီနေ့ မရှိသေးတဲ့ class တွေနဲ့ပါ အလုပ်လုပ်နိုင်တာပါ၊ နောက်လမှာ ရေးမယ့် Parrot class ကို ရှိပြီးသား code က ပြင်စရာ မလိုဘဲ လက်ခံပါလိမ့်မယ်။
- Declared type က ခေါ်လို့ရတာကို ကန့်သတ်
- Compiler က overload ကို ရွေးပြီးသား
- JVM က runtime class ကို ကြည့်
- အနီးဆုံး override က run တယ်
Field တွေနဲ့ static method တွေက ဒီ အဆင့် 3, 4 ကို မဖြတ်ပါဘူး၊ declared type ကနေပဲ ဆုံးဖြတ်ပါတယ်။
အထွေထွေ ပုံစံ
Parent ref = new Child(); // upcast: no cast needed
ref.overriddenMethod(); // runs Child's version
for (Parent item : listOfParents) {
item.overriddenMethod(); // each object picks its own version
}
if (ref instanceof Child child) { // safe downcast
child.childOnlyMethod();
}Parent type variable ထဲကို child object ကို ထည့်ပြီး override လုပ်ထားတဲ့ method ကို ခေါ်ပါ။ Child မှာပဲ ရှိတဲ့ method လိုမှသာ instanceof pattern နဲ့ စစ်ပြီး downcast လုပ်ပါ။
Worked example: Animal reference တစ်ခု၊ အသံ အမျိုးမျိုး
Dog နဲ့ Cat က Animal ရဲ့ makeSound() ကို override လုပ်ပါတယ်။ main က variable တွေကို Animal type နဲ့ပဲ ကြေညာပြီး List<Animal> ကို loop ပတ်ပါတယ်၊ ဒါပေမဲ့ object တစ်ခုချင်းစီက ကိုယ့် version ကို run ပါတယ်။ နောက်ဆုံးမှာ Dog မှာပဲ ရှိတဲ့ fetch() ကို ခေါ်ဖို့ instanceof pattern ကို သုံးထားပါတယ်။
import java.util.List;
public class Main {
public static void main(String[] args) {
Animal animal1 = new Dog();
Animal animal2 = new Cat();
animal1.makeSound();
animal2.makeSound();
List<Animal> shelter = List.of(new Cat(), new Animal(), new Dog());
for (Animal animal : shelter) {
animal.makeSound();
}
// animal1.fetch(); // compile error: Animal has no fetch()
if (animal1 instanceof Dog dog) {
dog.fetch();
}
}
}
class Animal {
public void makeSound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Dog says: Woof!");
}
public void fetch() {
System.out.println("Dog fetches the ball");
}
}
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Cat says: Meow!");
}
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Upcast
Animal animal1 = new Dog();Declared type က Animal၊ runtime class က Dog ပါ။ Dog က Animal တစ်မျိုး ဖြစ်လို့ cast မလိုပါဘူး။
- Override
@OverrideDog နဲ့ Cat က makeSound() ကို signature တူတူနဲ့ ပြန်ရေးထားပြီး @Override က တကယ် override ဖြစ်ကြောင်း compiler ကို စစ်ခိုင်းပါတယ်။
- One loop, three behaviours
animal.makeSound();Loop ထဲက line တစ်ကြောင်းတည်းက Cat, Animal, Dog အတွက် မတူတဲ့ code သုံးမျိုးကို run ပါတယ်။ Override မလုပ်ထားတဲ့ ရိုးရိုး Animal object ကတော့ parent version ကို run ပါတယ်။
- Safe downcast
if (animal1 instanceof Dog dog) {animal1.fetch() ကို တိုက်ရိုက် ရေးရင် Animal မှာ fetch() မရှိလို့ compile error ဖြစ်ပါတယ်။ instanceof pattern က object ကို စစ်ပြီး Dog ဖြစ်မှ dog variable ကို ပေးပါတယ်။
Dog says: Woof!
Cat says: Meow!
Cat says: Meow!
The animal makes a sound
Dog says: Woof!
Dog fetches the ball- Compile time: Animal မှာ makeSound() ရှိလား စစ်
- Runtime: animal ညွှန်ပြနေတဲ့ object ရဲ့ class ကို ရှာ
- အဲဒီ class မှာ override ရှိလား စစ်
- မရှိရင် parent ဆီ တက်ရှာ
Dog ဒါမှမဟုတ် Cat
Runs the subclass override
ရိုးရိုး Animal
Runs Animal.makeSound()
Overload က compile time၊ override က runtime
အောက်က program မှာ greet ဆိုတဲ့ static method နှစ်ခု (overload) ရှိပြီး speak() ကိုတော့ Dog က override လုပ်ထားပါတယ်။ Variable a ရဲ့ declared type က Animal ဖြစ်လို့ compiler က greet(Animal) ကို ရွေးလိုက်ပါပြီ၊ object က Dog ဖြစ်တာ အရေးမပါပါဘူး။ a.speak() ကတော့ runtime မှာ ရွေးတာမို့ Dog ရဲ့ version ကို run ပါတယ်။
public class Dispatch {
static void greet(Animal a) { System.out.println("greet(Animal)"); }
static void greet(Dog d) { System.out.println("greet(Dog)"); }
public static void main(String[] args) {
Animal a = new Dog();
greet(a); // overload: picked from the declared type
a.speak(); // override: picked from the runtime class
greet(new Dog()); // declared type Dog -> greet(Dog)
}
}
class Animal {
void speak() { System.out.println("..."); }
}
class Dog extends Animal {
@Override
void speak() { System.out.println("Woof"); }
}ဒီ program က greet(Animal), Woof, greet(Dog) ကို အစဉ်လိုက် print ထုတ်ပါတယ်။
Sealed type နဲ့ pattern matching switch (Java 21)
Override က behavior ကို class ထဲမှာ ထားချင်တဲ့အခါ အကောင်းဆုံးပါ။ ဒါပေမဲ့ ပုံဆွဲတာ၊ JSON ပြောင်းတာလို operation တွေကို class အပြင်ဘက်မှာ စုထားချင်တဲ့ အခါလည်း ရှိပါတယ်။ Java 17 ရဲ့ sealed class က subclass စာရင်းကို ပိတ်ထားပြီး Java 21 ရဲ့ pattern matching for switch က type တစ်ခုချင်းစီကို case တစ်ခုနဲ့ ကိုင်တွယ်ခွင့် ပေးပါတယ်။ စာရင်း ပိတ်ထားလို့ compiler က case အားလုံး ပါမပါ (exhaustive) စစ်ပေးနိုင်ပြီး default မလိုပါဘူး။
import java.util.List;
public class Shapes {
public static void main(String[] args) {
for (Shape shape : List.of(new Circle(1.5), new Square(2.0))) {
System.out.println(describe(shape));
}
}
static String describe(Shape shape) {
return switch (shape) {
case Circle c -> "circle, radius " + c.radius;
case Square s -> "square, side " + s.side;
};
}
}
sealed abstract class Shape permits Circle, Square {}
final class Circle extends Shape {
final double radius;
Circle(double radius) { this.radius = radius; }
}
final class Square extends Shape {
final double side;
Square(double side) { this.side = side; }
}abstract ကို ဘာကြောင့် ထည့်ရလဲ
Shape ကို sealed class လို့ပဲ ရေးပြီး abstract မထည့်ရင် new Shape() လို ရိုးရိုး Shape object တစ်ခု ရှိနိုင်သေးလို့ compiler က "the switch expression does not cover all possible input values" လို့ ငြင်းပါတယ်။ Subclass အသစ် ထည့်တဲ့အခါ permits ကို ပြင်ရပြီး switch တိုင်းက compile error နဲ့ ပြင်ဖို့ သတိပေးပါတယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ polymorphism
Business app တွေမှာ "တူညီတဲ့ လုပ်ဆောင်ချက်၊ မတူတဲ့ အကောင်အထည်ဖော်ပုံ" ဆိုတဲ့ ပုံစံကို နေရာတိုင်းမှာ တွေ့ရပါတယ်။ Checkout code က payment.charge(amount) လို့ပဲ ခေါ်ပြီး card, bank transfer, mobile wallet ဘယ်ဟာ ဖြစ်မလဲဆိုတာ runtime object က ဆုံးဖြတ်ပါတယ်။ Report export ကလည်း exporter.write(rows) တစ်ကြောင်းတည်းနဲ့ CSV, PDF, Excel exporter တွေကို ကိုင်တွယ်နိုင်ပါတယ်။
- JDK ထဲမှာ System.out.println(object) က object.toString() ကို ခေါ်ပါတယ်။ ကိုယ့် class မှာ toString() ကို override လုပ်လိုက်တာနဲ့ println, string concatenation, log message အားလုံးက ကိုယ့် version ကို သုံးသွားပါတယ်။
- Collection framework မှာ List<String> names = new ArrayList<>(); လို့ ရေးပြီး နောက်ပိုင်း LinkedList နဲ့ လဲချင်ရင် ညာဘက်ခြမ်း တစ်နေရာတည်းကိုပဲ ပြင်ရပါတယ်၊ ကျန်တဲ့ code က List ကိုပဲ သိလို့ပါ။
- Test တွေမှာ အမှန်တကယ် email ပို့တဲ့ class နေရာမှာ message ကို list ထဲ မှတ်ထားရုံပဲ လုပ်တဲ့ fake subclass ကို ထည့်ပြီး ပို့ခဲ့တဲ့ message တွေကို စစ်ဆေးလို့ ရပါတယ်။
အမှားများ
Polymorphism နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ
Override ထင်ပြီး overload လုပ်မိခြင်း
Dog class ထဲမှာ public boolean equals(Dog other) လို့ ရေးရင် Object ရဲ့ equals(Object) ကို override မလုပ်ဘဲ overload အသစ် ဖြစ်သွားပါတယ်။ List.contains() လို code တွေက equals(Object) ကို ခေါ်လို့ သင့် method ကို ဘယ်တော့မှ မရောက်ပါဘူး။ @Override ကို ထည့်ထားရင် compiler က ချက်ချင်း ဖမ်းပေးပါတယ်။
မစစ်ဘဲ downcast လုပ်ခြင်း
Animal a = new Cat(); Dog d = (Dog) a; က compile ဖြစ်ပါတယ်၊ ဒါပေမဲ့ run တဲ့အခါ ClassCastException နဲ့ crash ဖြစ်ပါတယ်။ Downcast လုပ်ဖို့ လိုရင် if (a instanceof Dog d) နဲ့ စစ်ပါ။ ဒါပေမဲ့ downcast တွေ မကြာခဏ လိုနေရင် လိုအပ်တဲ့ method ကို parent type မှာ ထည့်သင့်တယ်ဆိုတဲ့ လက္ခဏာပါ။
Static method နဲ့ field တွေကို polymorphic ထင်ခြင်း
Parent နဲ့ child နှစ်ခုလုံးမှာ static void info() ရှိပြီး Parent p = new Child(); p.info(); လို့ ခေါ်ရင် Parent ရဲ့ version ပဲ run ပါတယ်၊ static method တွေက override မဖြစ်ဘဲ hide ပဲ ဖြစ်လို့ပါ။ နာမည်တူ field တွေမှာလည်း ဒီအတိုင်းပါပဲ။ Runtime မှာ ပြောင်းလဲစေချင်တဲ့ behavior ကို instance method အဖြစ်ပဲ ရေးပါ။
Best Practices
Best practices
- Variable နဲ့ parameter တွေကို လိုအပ်တဲ့ အကျယ်ဆုံး type (List, Animal) နဲ့ ကြေညာပါ။
- instanceof chain မရေးခင် behavior ကို override method အဖြစ် ရွှေ့လို့ ရမလား အရင် စဉ်းစားပါ။
- Subclass စာရင်း ပိတ်ထားပြီး class အပြင်ဘက်မှာ operation ရေးချင်ရင် sealed type နဲ့ pattern switch ကို သုံးပါ။
- Override လုပ်တဲ့ method တိုင်းမှာ @Override ကို ထည့်ပါ။
Mini challenge
Mini challenge: လက်မှတ် ဈေးနှုန်း
ပြပွဲ လက်မှတ် စနစ်တစ်ခုအတွက် Ticket class နဲ့ ChildTicket, SeniorTicket subclass နှစ်ခု ရေးပါ။ Ticket ရဲ့ ဈေးက 10.00 ဖြစ်ပြီး ChildTicket က 5.00၊ SeniorTicket ကတော့ ပုံမှန်ဈေးရဲ့ 70% ဖြစ်ပါတယ်။ List<Ticket> တစ်ခုကို loop ပတ်ပြီး လက်မှတ်တစ်စောင်ချင်းစီရဲ့ ဈေးနဲ့ စုစုပေါင်းကို print ထုတ်ပါ။
- Subclass နှစ်ခုလုံးက price() ကို @Override နဲ့ override လုပ်ရမယ်
- SeniorTicket က super.price() ကို ပြန်သုံးရမယ် (10.00 ကို ထပ်မရေးရ)
- Loop က instanceof မသုံးဘဲ ticket.price() ကိုပဲ ခေါ်ရမယ်
- Class နာမည်ကို getClass().getSimpleName() နဲ့ print ထုတ်ပါ
import java.util.List;
public class Tickets {
public static void main(String[] args) {
List<Ticket> basket = List.of(new Ticket(), new ChildTicket(), new SeniorTicket());
// TODO: loop, print "<ClassName>: <price with 2 decimals>" and add to a total
// TODO: print "Total: <total with 2 decimals>"
}
}
class Ticket {
double price() {
// TODO
}
}
// TODO: ChildTicket overrides price() -> 5.00
// TODO: SeniorTicket overrides price() -> 70% of super.price()ရလဒ်: Ticket: 10.00, ChildTicket: 5.00, SeniorTicket: 7.00 ဆိုတဲ့ line သုံးကြောင်းနဲ့ နောက်ဆုံးမှာ Total: 22.00 ထွက်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
printf("%s: %.2f%n", name, price) က format နှစ်ခုလုံးကို ကိုင်တွယ်ပေးပါတယ်။ Loop variable ရဲ့ type က Ticket ဖြစ်ပေမဲ့ price() ခေါ်တိုင်း object ရဲ့ ကိုယ်ပိုင် version run မှာ ဖြစ်လို့ instanceof လုံးဝ မလိုပါဘူး။
အဖြေ ကြည့်ရန်
import java.util.List;
public class Tickets {
public static void main(String[] args) {
List<Ticket> basket = List.of(new Ticket(), new ChildTicket(), new SeniorTicket());
double total = 0;
for (Ticket ticket : basket) {
double price = ticket.price();
System.out.printf("%s: %.2f%n", ticket.getClass().getSimpleName(), price);
total += price;
}
System.out.printf("Total: %.2f%n", total);
}
}
class Ticket {
double price() {
return 10.00;
}
}
class ChildTicket extends Ticket {
@Override
double price() {
return 5.00;
}
}
class SeniorTicket extends Ticket {
@Override
double price() {
return super.price() * 0.7;
}
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Variable တစ်ခုမှာ declared type နဲ့ runtime class ဆိုပြီး type နှစ်ခု ရှိပါတယ်။
- Declared type က ခေါ်လို့ရတဲ့ method တွေကို ကန့်သတ်ပြီး runtime class က ဘယ် override run မလဲ ဆုံးဖြတ်ပါတယ်။
- Overload ရွေးချယ်မှု၊ field နဲ့ static method တွေက compile time မှာ declared type နဲ့ ဆုံးဖြတ်ပါတယ်။
- Downcast ကို instanceof pattern နဲ့ စစ်ပြီးမှ လုပ်ပါ၊ subclass စာရင်း ပိတ်ထားရင် sealed type နဲ့ pattern switch ကို သုံးနိုင်ပါတယ်။