Polymorphism: dynamic dispatch and toString() · Polymorphism: dynamic dispatch และ toString()
Polymorphism: one type, many behaviours
- Polymorphism means "many forms": one superclass type, many subclass behaviours.
- You can hold a
Dogor aCatin anAnimalvariable, because each is-anAnimal. - When you call a method, the object's real type decides which version runs.
Polymorphism: หนึ่งประเภท หลายพฤติกรรม
- Polymorphism หมายถึง "หลายรูปแบบ": หนึ่ง superclass type, หลาย subclass behaviors
- คุณสามารถถือ一个
Dogหรือ一个CatในตัวแปรAnimalได้ เพราะแต่ละ เป็น-anAnimal - เมื่อคุณเรียกメソッド, real type ของ object ตัดสินว่าเวอร์ชันไหนจะรัน
Dynamic dispatch
- The variable's type (
Animal) decides which methods you are allowed to call. - But at run time, the actual object (
Dog) decides which overridden version runs. - This run-time choice is called dynamic dispatch — it is the heart of polymorphism.
การส่งต่อแบบไดนามิก (Dynamic dispatch)
- ประเภทของตัวแปร (
Animal) เป็นตัวกำหนดว่าคุณ สามารถ เรียกใช้เมทόδใดได้ - แต่ในช่วงรันไทม์ ออบเจ็กต์ที่แท้จริง (
Dog) คือผู้ตัดสินใจว่าเวอร์ชันที่ ถูก Override ตัวไหนจะทำงาน - การเลือกนี้ในช่วงรันไทม์เรียกว่า dynamic dispatch — ซึ่งเป็นหัวใจสำคัญของ Polymorphism
public class Main {
public static void main(String[] args) {
Animal[] zoo = { new Dog(), new Cat(), new Dog() };
for (Animal a : zoo) {
System.out.println(a.speak()); // Woof, then Meow, then Woof
}
}
}
class Animal {
public String speak() { return "some sound"; }
}
class Dog extends Animal {
public String speak() { return "Woof"; }
}
class Cat extends Animal {
public String speak() { return "Meow"; }
}
One loop handles every subclass
- The loop above never asks "is this a Dog or a Cat?" — it just calls
speak(). - Each object answers in its own way. That keeps the loop short and general.
- Add a brand-new subclass later and the same loop works on it, unchanged.
ลูปเดียวจัดการทุก Subclass ได้
- ลูปข้างต้นไม่ถามเลยว่า "นี่คือ Dog หรือ Cat?" — มันแค่เรียก
speak() - แต่ละออบเจ็กต์ตอบกลับใน รูปแบบของมันเอง ซึ่งช่วยให้ลูปสั้นลงและเป็นสากลมากขึ้น
- หากเพิ่ม Subclass ใหม่ในอนาคต ลูปเดิม ก็ยังทำงานได้อย่างไม่มีอะไรเปลี่ยนไป
toString(): every object inherits it from Object
- Every class in Java secretly extends a class called
Object, which has atoString(). - Override
toString()to say how your object should look as text. - Java calls it automatically when you print an object or join it with
+.
toString(): ทุกออบเจ็กต์สืบทอดมาจาก Object
- ทุก Class ใน Java สืบทอดจาก Class ที่ชื่อว่า
Objectโดยอัตโนมัติ ซึ่งมีtoString() - Override
toString()เพื่อบอกว่าออบเจ็กต์ของคุณควรแสดงเป็นข้อความอย่างไร - Java จะเรียกมันอัตโนมัติเมื่อคุณพิมพ์วัตถุหรือรวม它与
+เข้าด้วยกัน
public class Main {
public static void main(String[] args) {
Object x = new Fraction(3, 4);
System.out.println(x); // 3/4 -- toString() runs
System.out.println("ratio " + x); // ratio 3/4
}
}
class Fraction {
private int numerator;
private int denominator;
public Fraction(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public String toString() {
return numerator + "/" + denominator;
}
}
Common mistakes
- The actual object's method runs at run time (dynamic dispatch), not the variable's type.
- Override
toString()for readable output.
ข้อผิดพลาดที่พบบ่อย
- เมทόδของออบเจ็กต์ที่แท้จริงจะทำงานในช่วงรันไทม์ (dynamic dispatch) ไม่ใช่ตามประเภทของตัวแปร
- Override
toString()เพื่อให้ได้ผลลัพธ์ที่อ่านง่าย
Now you try
- Use a superclass array, add a new subclass, and override
toString(). - A hidden Harness checks that the right version runs for each object.
- Press Run to compile, then Check answer. Your code runs on the server, so even the first run is fast.
ลองดูเลย
- ใช้ Array ของ Superclass เพิ่ม Subclass ใหม่ และ Override
toString() - There is a hidden Harness that checks the correct version runs for each object.
- กด Run เพื่อคอมไพล์ แล้ว Check answer โค้ดของคุณรันบน server ดังนั้นแม้แต่ครั้งแรกก็เร็ว
Animal, Dog, and Cat are written for you. Complete the static method Zoo.chorus(Animal[] animals) so it returns every animal's sound joined by single spaces. Example: a Dog, Cat, Dog array gives "Woof Meow Woof". An empty array gives "". Loop and call speak() — let polymorphism pick the right sound. · Animal, Dog และ Cat ถูกเขียนไว้ให้แล้ว เติมเต็ม method แบบ static Zoo.chorus(Animal[] animals) เพื่อให้กลับเสียงของสัตว์ทุกชนิดที่เชื่อมกันด้วยช่องว่างเดียว ตัวอย่าง: อาร์เรย์ Dog, Cat, Dog จะให้ผลลัพธ์ "Woof Meow Woof" อาร์เรย์ว่างจะให้ "" ทำ loop และเรียก speak() — ให้ polymorphism เลือกเสียงที่ถูกต้อง
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Animal, Dog, Cat, and a working Zoo.chorus are written for you. Add a class Cow that extends Animal and overrides speak() to return "Moo". Notice the existing chorus loop handles your new Cow with no changes. · Animal, Dog, Cat และ Zoo.chorus ที่ทำงานได้แล้วถูกเขียนไว้ให้แล้ว เพิ่มคลาส Cow ที่ extends Animal และ override speak() เพื่อกลับ "Moo" ระวังว่า loop ของ chorus ที่มีอยู่สามารถจัดการกับ Cow ใหม่ของคุณได้ โดยไม่ต้องแก้ไขอะไรเลย
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete Fraction by overriding toString() so it returns the fraction as "numerator/denominator" — for 3 and 4 it returns "3/4". Java calls toString() automatically when you print a Fraction or join it with +. · เติมเต็ม Fraction โดยการ override toString() เพื่อกลับเศษส่วนในรูป "numerator/denominator" — สำหรับ 3 และ 4 จะกลับ "3/4" Java จะเรียก toString() อัตโนมัติเมื่อคุณพิมพ์ Fraction หรือต่อมันเข้ากับ +
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่