Thuta Learning
Java
IntermediateProgrammingအခြေခံ

Method Overloading

Javaသင်ခန်းစာ 1425 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Method signature (နာမည် + parameter type တွေ) ဆိုတာ ဘာလဲ ရှင်းပြနိုင်ဖို့
  • Parameter အရေအတွက်၊ type ဒါမှမဟုတ် အစဉ် မတူတဲ့ overload တွေ ရေးနိုင်ဖို့
  • Widening, boxing, varargs အဆင့်တွေနဲ့ ဘယ် overload ရွေးခံရမလဲ ကြိုတွက်နိုင်ဖို့
  • Return type တစ်ခုတည်း ကွာတာနဲ့ ambiguous call error တွေကို မှတ်မိနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

ကော်ဖီဆိုင်မှာ "ကော်ဖီ တစ်ခွက်" လို့ မှာလည်း ရ၊ "ကော်ဖီ နှစ်ခွက်" လို့ မှာလည်း ရ၊ "ကော်ဖီ နှစ်ခွက်၊ နို့ဆီ မပါ" လို့ မှာလည်း ရပါတယ်။ ဝန်ထမ်းက နာမည် တစ်ခုတည်း (ကော်ဖီ) ကို ကြားပေမယ့် နောက်က ပါလာတဲ့ အချက်အလက်ကို ကြည့်ပြီး ဘာလုပ်ရမလဲ သိပါတယ်။

Method overloading ကလည်း ဒီလိုပါပဲ။ add(int, int), add(double, double), add(int, int, int) လို နာမည်တူ method တွေကို parameter list မတူအောင် ရေးထားလို့ ရပါတယ်။ add(5, 3) လို့ ခေါ်လိုက်တဲ့အခါ compiler က argument တွေရဲ့ အရေအတွက်နဲ့ type ကို ကြည့်ပြီး ကိုက်ညီဆုံး version ကို ရွေးပေးပါတယ်။ ဒါကြောင့် addInts, addDoubles, addThree လို နာမည် အမျိုးမျိုး မှတ်စရာ မလိုတော့ပါဘူး။

Problem → Why → Solution

ပြဿနာ: return type ကွာရုံနဲ့ overload လို့ ထင်တာ

Developer တစ်ယောက်က parse() ကို int ပြန်တဲ့ version နဲ့ double ပြန်တဲ့ version နှစ်ခု လိုချင်လို့ အောက်ကလို ရေးလိုက်ပါတယ်။ Parameter list က (String text) အတူတူ ဖြစ်နေလို့ javac က "method parse(String) is already defined in class Main" ဆိုပြီး compile မလုပ်ပေးပါဘူး။

Main.java
public class Main {
  static int parse(String text) {
    return Integer.parseInt(text);
  }

  static double parse(String text) { // error: already defined
    return Double.parseDouble(text);
  }

  public static void main(String[] args) {
    System.out.println(parse("42")); // which one should run?
  }
}

ဘာကြောင့် ဒါကို ခွင့်မပြုတာလဲ

Compiler က overload ကို ခေါ်တဲ့နေရာမှာ ပါတဲ့ argument တွေကနေပဲ ရွေးပါတယ်။ parse("42") မှာ argument က String တစ်ခုပဲ ဖြစ်လို့ ဘယ် version ကို ဆိုလိုမှန်း သိဖို့ နည်းလမ်း မရှိပါဘူး — ရလဒ်ကို println ထဲ တိုက်ရိုက် ပို့တာလို နေရာတွေမှာ return type ကို သုံးပြီး ခန့်မှန်းလို့လည်း မရပါဘူး။ ဒါကြောင့် signature ထဲမှာ return type မပါဘဲ parseInt နဲ့ parseDouble လို နာမည် ခွဲပေးရပါတယ်။

Core concept: signature နဲ့ compile-time ရွေးချယ်မှု

Java မှာ method တစ်ခုရဲ့ signature ဆိုတာ နာမည်နဲ့ parameter type တွေရဲ့ အစဉ်ပါ။ Class တစ်ခုထဲမှာ signature တူတဲ့ method နှစ်ခု မရှိရပါဘူး။ ဒါကြောင့် overload တွေဟာ parameter အရေအတွက် (add(int, int) နဲ့ add(int, int, int))၊ type (add(int, int) နဲ့ add(double, double)) ဒါမှမဟုတ် အစဉ် (f(int, String) နဲ့ f(String, int)) ကွာရပါမယ်။ Return type, parameter နာမည်တွေ နဲ့ static ရှိမရှိကတော့ signature ထဲ မပါလို့ အဲဒါတွေ ကွာရုံနဲ့ overload မဖြစ်ပါဘူး။

ဘယ် overload ကို ခေါ်မလဲဆိုတာ program run နေချိန်မှာ မဟုတ်ဘဲ compile လုပ်ချိန်မှာ ဆုံးဖြတ်ပါတယ်။ Compiler က argument expression တွေရဲ့ declared (static) type ကို ကြည့်ပြီး အဆင့်သုံးဆင့်နဲ့ ရှာပါတယ်။ ပထမအဆင့်မှာ boxing နဲ့ varargs မသုံးဘဲ type တိတိကျကျ ကိုက်တာ ဒါမှမဟုတ် int ကနေ long/double လို widening နဲ့ ကိုက်တာကို ရှာပါတယ်။ မတွေ့မှ ဒုတိယအဆင့်မှာ int ↔ Integer boxing/unboxing ကို ခွင့်ပြုပြီး တတိယအဆင့်ကျမှ varargs (int...) ကို စဉ်းစားပါတယ်။ ဒါကြောင့် add(5, 2.5) က 5 ကို 5.0 အဖြစ် widen လုပ်ပြီး add(double, double) ကို ရွေးပါတယ်။

အဆင့်တစ်ခုထဲမှာ ကိုက်တဲ့ overload တစ်ခုထက် ပိုရှိရင် compiler က အတိကျဆုံး (most specific) ကို ရွေးပါတယ် — ဥပမာ describe(Object) နဲ့ describe(String) ရှိရင် describe("hi") က String version ကို ရပါတယ်။ ဘယ်ဟာမှ အတိကျဆုံး မဖြစ်ရင်တော့ "reference to f is ambiguous" error ပေးပါတယ်။ အရေးကြီးတာက overload ကို argument ရဲ့ declared type နဲ့ ရွေးတာပါ၊ runtime မှာ ရှိနေတဲ့ object type နဲ့ မဟုတ်ပါဘူး။ Object x = "hi"; describe(x); ဆိုရင် Object version ကို ခေါ်ပါတယ်။

Overloading ကို overriding နဲ့ မရောပါနဲ့။ Overloading က class တစ်ခုထဲမှာ နာမည်တူ signature မတူ method အသစ်တွေ ထပ်ထည့်တာဖြစ်ပြီး compile time မှာ ရွေးပါတယ်။ Overriding ကတော့ subclass က parent ရဲ့ signature အတူတူ method ကို အစားထိုးတာဖြစ်ပြီး runtime မှာ object type နဲ့ ရွေးပါတယ် — Inheritance lesson မှာ အသေးစိတ် တွေ့ရပါမယ်။ Constructor တွေကိုလည်း ဒီ rule အတိုင်းပဲ overload လုပ်လို့ရတာကို Constructors lesson မှာ သုံးပါမယ်။

မှတ်ထားရမယ့် ပုံစံ: compiler ရဲ့ ရှာဖွေမှု
  1. နာမည်တူ method အားလုံးကို စုပါ
  2. Exact ဒါမှမဟုတ် widening နဲ့ ကိုက်တာ ရှာ
  3. မတွေ့ရင် boxing ကို ခွင့်ပြု
  4. မတွေ့သေးရင် varargs ကို ခွင့်ပြု
  5. အတိကျဆုံးကို ရွေး၊ မရရင် ambiguous

အဆင့်တစ်ခုမှာ ကိုက်တာ တွေ့တာနဲ့ နောက်အဆင့်တွေကို လုံးဝ မကြည့်တော့ပါဘူး။

အထွေထွေ ပုံစံ

java
static Result name(TypeA a) { ... }           // one parameter
static Result name(TypeA a, TypeB b) { ... }  // different count
static Result name(TypeB b, TypeA a) { ... }  // different order
static Result name(TypeC c) { ... }           // different type
static Result name(TypeA... many) { ... }     // varargs, chosen last

// NOT an overload: only the return type differs
static Other name(TypeA a) { ... }            // compile error

Overload တိုင်းမှာ နာမည် တူပြီး parameter type list မတူရပါမယ်။ Return type တစ်ခုတည်း ကွာတာက already defined error ပါ။ Varargs version ကို compiler က နောက်ဆုံးမှသာ စဉ်းစားပါတယ်။

Example: add() overload သုံးခု

ဒီ program မှာ add နာမည်နဲ့ method သုံးခု ရှိပါတယ်။ main ထဲက call လေးခုစီအတွက် compiler က ဘယ် version ကို ရွေးမလဲ အရင် ခန့်မှန်းကြည့်ပါ — နောက်ဆုံး add(5, 2.5) က အရင်ဆုံး မျက်စိလည်စရာ ဖြစ်နိုင်ပါတယ်။ Output ထဲမှာ int ရလဒ်က 8 လို ဒသမ မပါဘဲ ထွက်ပြီး double ရလဒ်က 7.5 လို ဒသမ ပါတာကို သတိထားပါ။

Main.java
public class Main {
  static int add(int a, int b) {
    return a + b;
  }

  static double add(double a, double b) {
    return a + b;
  }

  static int add(int a, int b, int c) {
    return a + b + c;
  }

  public static void main(String[] args) {
    System.out.println(add(5, 3));
    System.out.println(add(2.5, 4.2));
    System.out.println(add(1, 2, 3));
    System.out.println(add(5, 2.5)); // 5 is widened to 5.0
  }
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Two intsSystem.out.println(add(5, 3));

    Argument နှစ်ခုလုံး int ဖြစ်လို့ add(int, int) က exact match ပါ။ add(double, double) ကလည်း widening နဲ့ ကိုက်ပေမယ့် int version က ပိုတိကျလို့ အဲဒါကို ရွေးပြီး 8 ထွက်ပါတယ်။

  2. Two doublesstatic double add(double a, double b) {

    2.5 နဲ့ 4.2 က double literal တွေဖြစ်ပြီး double ကို int အဖြစ် အလိုအလျောက် ချုံ့ (narrow) မပေးလို့ ဒီ version တစ်ခုတည်းပဲ ကိုက်ပါတယ်။ ရလဒ်က 6.7 ပါ။

  3. Different countstatic int add(int a, int b, int c) {

    Argument သုံးခုနဲ့ ခေါ်တဲ့ add(1, 2, 3) ကို parameter သုံးခု ရှိတဲ့ ဒီ version ကပဲ လက်ခံနိုင်ပါတယ်။ အရေအတွက် ကွာတာက overload ကို ခွဲခြားဖို့ အရှင်းဆုံး နည်းပါ။

  4. Mixed int and doubleSystem.out.println(add(5, 2.5)); // 5 is widened to 5.0

    add(int, int) က 2.5 ကို မလက်ခံနိုင်ပါဘူး။ add(double, double) ကတော့ 5 ကို 5.0 အဖြစ် widen လုပ်ပြီး ပထမအဆင့်မှာပဲ ကိုက်လို့ 7.5 ထွက်ပါတယ်။

You should see
8
6.7
6
7.5

Widening, boxing, varargs: ဘယ်ဟာ အရင်လဲ

အောက်က program မှာ show(long), show(Integer), show(int...) သုံးခု ရှိပါတယ်။ show(5) မှာ int 5 ကို Integer အဖြစ် box လုပ်ရင် ပိုနီးစပ်တယ်လို့ ထင်စရာ ရှိပေမယ့် widening (int → long) က ပထမအဆင့်မှာ ရှိလို့ long version က နိုင်ပါတယ်။ Argument နှစ်ခု ဒါမှမဟုတ် တစ်ခုမှ မပါတဲ့ call တွေကိုတော့ varargs version ကပဲ လက်ခံနိုင်ပါတယ်။ နောက်ဆုံး describe() call သုံးခုက declared type နဲ့ ရွေးတာကို ပြပါတယ်။

Main.java
public class Main {
  static void show(long x) { System.out.println("long " + x); }
  static void show(Integer x) { System.out.println("Integer " + x); }
  static void show(int... xs) { System.out.println("varargs " + xs.length); }

  static void describe(Object o) { System.out.println("Object version"); }
  static void describe(String s) { System.out.println("String version"); }

  public static void main(String[] args) {
    show(5);     // phase 1: widening int -> long beats boxing
    show(5, 6);  // only varargs accepts two ints
    show();      // only varargs accepts zero arguments

    Object x = "hello";
    describe(x);        // declared type is Object
    describe("hello");  // declared type is String
    describe(null);     // String is more specific than Object
  }
}
Callရွေးခံရတဲ့ methodအကြောင်းရင်း
show(5)show(long)Widening က boxing ထက် အရင်
show(5, 6)show(int...)Varargs တစ်ခုတည်းသာ ကိုက်
describe(x)describe(Object)x ရဲ့ declared type က Object
describe(null)describe(String)String က ပိုတိကျ

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

JDK ကိုယ်တိုင်က overloading ကို နေရာတိုင်းမှာ သုံးပါတယ်။ System.out.println() မှာ int, double, char, String, Object စသဖြင့် overload ဆယ်ခုလောက် ရှိလို့ ဘာ type ပဲ ပို့ပို့ println လို့ပဲ ခေါ်ရပါတယ်။ Math.max(), Math.abs() တို့မှာလည်း int, long, float, double version တွေ ရှိပြီး List.of() မှာတော့ element 0 ကနေ 10 ခုအထိ fixed-parameter overload တွေနဲ့ အဲဒီထက်များရင် varargs version ရှိပါတယ်။

ကိုယ့် code မှာတော့ default value ပေးတဲ့ convenience overload တွေ အသုံးများပါတယ် — sendEmail(to, subject) က sendEmail(to, subject, "no-reply@shop.com") ကို ခေါ်ပေးတာမျိုးပါ။ Java မှာ default parameter မရှိလို့ ဒီ pattern က အစားထိုးပါတယ်။ ဒါပေမယ့် သတိထားစရာ ဥပမာ တစ်ခုက List<Integer> ပါ: list.remove(1) က remove(int index) ကို ရွေးလို့ value 1 ကို မဟုတ်ဘဲ index 1 က element ကို ဖျက်ပါတယ်။ Value ကို ဖျက်ချင်ရင် list.remove(Integer.valueOf(1)) လို့ ရေးမှ remove(Object) ကို ရပါတယ်။ Production bug တွေ အများကြီး ဒီကနေ စခဲ့ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Return type ဒါမှမဟုတ် parameter နာမည်ပဲ ပြောင်းတာ

static int add(int a, int b) နဲ့ static double add(int x, int y) က signature တူတူ (add(int, int)) ဖြစ်လို့ "method add(int,int) is already defined" error တက်ပါတယ်။ Parameter type တစ်ခုခု ပြောင်းပါ၊ ဒါမှမဟုတ် နာမည်ကို addExact လို ခွဲပေးပါ။

2. Ambiguous call ဖြစ်အောင် design လုပ်မိတာ

f(int a, double b) နဲ့ f(double a, int b) နှစ်ခုရှိရင် f(1, 2) က နှစ်ခုလုံးနဲ့ widening တစ်ခုစီနဲ့ ကိုက်ပြီး ဘယ်ဟာမှ ပိုမတိကျလို့ "reference to f is ambiguous" ဖြစ်ပါတယ်။ ဒီလို အစဉ်ပဲ ကွာတဲ့ overload တွေကို ရှောင်ပါ။

3. Runtime object type နဲ့ ရွေးမယ်ထင်တာ

Object x = "hi"; describe(x); က describe(String) ကို ခေါ်မယ်ထင်တာပါ။ Overload ကို compile time မှာ declared type (Object) နဲ့ ရွေးပြီးသားမို့ Object version ပဲ run ပါတယ်။ Runtime type နဲ့ ရွေးချင်ရင် နောက် lesson တွေက overriding နဲ့ polymorphism ကို သုံးရပါတယ်။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Overload တိုင်းက အဓိပ္ပာယ် တူတဲ့ အလုပ်ကိုပဲ လုပ်ရပါမယ်၊ အလုပ်ကွဲရင် နာမည်ခွဲပါ။
  • တိုတဲ့ overload တွေက အပြည့်အစုံ version ကို ခေါ်ပြီး logic ကို တစ်နေရာတည်းမှာ ထားပါ။
  • Integer နဲ့ int, Object နဲ့ String လို ပြောင်းလို့ရတဲ့ type တွေနဲ့ အနေအထား တူ overload တွေ မရေးပါနဲ့။

Mini challenge

Mini challenge: receipt label overload များ

ကော်ဖီဆိုင် receipt အတွက် label() overload သုံးခုနဲ့ varargs sum() တစ်ခု ရေးပါ။ တိုတဲ့ overload တွေက ကိုယ်ပိုင် formatting မရေးဘဲ အပြည့်အစုံ version ကို ခေါ်ရပါမယ်။

  • label(String item, int quantity, double unitPrice) က "Cake x3 @ 2500.0 = 7500.0" ပုံစံ String ပြန်ရမယ်
  • label(String item, int quantity) က DEFAULT_PRICE (1000.0) ကို သုံးပြီး အပြည့်အစုံ version ကို ခေါ်ရမယ်
  • label(String item) က quantity 1 နဲ့ နှစ်ခုပါ version ကို ခေါ်ရမယ်
  • sum(int... amounts) က argument မပါရင် 0 ပြန်ရမယ်
Main.java
public class Main {
  static final double DEFAULT_PRICE = 1000.0;

  // TODO: label(String item, int quantity, double unitPrice)
  //       -> "Cake x3 @ 2500.0 = 7500.0"

  // TODO: label(String item, int quantity) -> delegate with DEFAULT_PRICE

  // TODO: label(String item) -> delegate with quantity 1

  // TODO: sum(int... amounts) -> total of all amounts, 0 when empty

  public static void main(String[] args) {
    // TODO: print label("Tea"), label("Coffee", 2), label("Cake", 3, 2500.0)
    // TODO: print sum() and sum(1500, 2500, 800)
  }
}

ရလဒ်: ဒီလို ငါးကြောင်း ထွက်ရပါမယ်: Tea x1 @ 1000.0 = 1000.0 Coffee x2 @ 1000.0 = 2000.0 Cake x3 @ 2500.0 = 7500.0 No items: 0 Bill: 4800

အရိပ်အမြွက် ကြည့်ရန်

Varargs parameter int... amounts ကို method ထဲမှာ int[] လိုပဲ for-each နဲ့ ပတ်လို့ရပါတယ်။ label("Tea") က label("Tea", 1) ကို၊ အဲဒါက label("Tea", 1, DEFAULT_PRICE) ကို ဆက်ခေါ်တဲ့ ကွင်းဆက်ပုံစံ ဖြစ်ပါတယ်။

အဖြေ ကြည့်ရန်
Main.java
public class Main {
  static final double DEFAULT_PRICE = 1000.0;

  static String label(String item, int quantity, double unitPrice) {
    double total = quantity * unitPrice;
    return item + " x" + quantity + " @ " + unitPrice + " = " + total;
  }

  static String label(String item, int quantity) {
    return label(item, quantity, DEFAULT_PRICE);
  }

  static String label(String item) {
    return label(item, 1);
  }

  static int sum(int... amounts) {
    int total = 0;
    for (int amount : amounts) {
      total += amount;
    }
    return total;
  }

  public static void main(String[] args) {
    System.out.println(label("Tea"));
    System.out.println(label("Coffee", 2));
    System.out.println(label("Cake", 3, 2500.0));
    System.out.println("No items: " + sum());
    System.out.println("Bill: " + sum(1500, 2500, 800));
  }
}

Quick Quiz 1 / 3

static void print(long x) နဲ့ static void print(Integer x) နှစ်ခုရှိရင် print(7); က ဘယ်ဟာကို ခေါ်မလဲ။

Quick Quiz 2 / 3

Class တစ်ခုထဲမှာ အောက်က pair တွေထဲက ဘယ်ဟာက compile error ဖြစ်မလဲ။

Quick Quiz 3 / 3

List<Integer> list = new ArrayList<>(List.of(10, 20, 30)); list.remove(1); ပြီးရင် list က ဘာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Signature = method နာမည် + parameter type တွေရဲ့ အစဉ်၊ return type နဲ့ parameter နာမည် မပါပါဘူး။
  • Overload တွေက parameter အရေအတွက်၊ type ဒါမှမဟုတ် အစဉ် ကွာရပါမယ်။
  • Compiler က declared type ကို ကြည့်ပြီး exact/widening → boxing → varargs အစဉ်နဲ့ compile time မှာ ရွေးပါတယ်။
  • ဘယ် overload မှ အတိကျဆုံး မဖြစ်ရင် ambiguous error ဖြစ်ပြီး runtime object type က ရွေးချယ်မှုကို မပြောင်းပါဘူး။

Java Language Specification, Java SE 25 — Oracle

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Method Overloading | Thuta Learning