Inheritance: extends, super, and overriding · การสืบทอด (Inheritance): extends, super และการแทนที่ (overriding)
Inheritance: building on a class
- Inheritance lets one class reuse another class's fields and methods.
- The class you build on is the superclass (parent); the new class is the subclass (child).
- A subclass is-a kind of its superclass: a
Dogis anAnimal.
การสืบทอด: การสร้างบนคลาส
- การสืบทอด ทำให้คลาสหนึ่งนำฟิลด์และเมทอดของอีกคลาสมาใช้ซ้ำได้
- คลาสที่เราสร้างบน它就是 superclass (พ่อ); คลาสใหม่คือ subclass (ลูก)
- subclass เป็น-a ประเภทหนึ่งของ superclass:一个
Dogเป็น-anAnimal
extends and super
- Write
class Dog extends Animalto makeDoga subclass ofAnimal. Dogautomatically gets every public method ofAnimal, likegetName().- The subclass constructor calls the superclass constructor first with
super(...).
extends และ super
- เขียน
class Dog extends Animalเพื่อทำให้Dogเป็น subclass ของAnimal Dogจะได้รับทุก public method ของAnimalโดยอัตโนมัติ เช่นgetName()- Constructor ของ subclass จะเรียก constructor ของ superclass ก่อนด้วย
super(...)
public class Main {
public static void main(String[] args) {
Dog d = new Dog("Rex");
System.out.println(d.getName()); // Rex (inherited from Animal)
System.out.println(d.speak()); // Woof (Dog's own version)
}
}
class Animal {
private String name;
public Animal(String name) { this.name = name; }
public String getName() { return this.name; }
public String speak() { return "some sound"; }
}
class Dog extends Animal {
public Dog(String name) {
super(name); // run Animal's constructor
}
public String speak() { // override Animal's speak()
return "Woof";
}
}
Overriding a method
- A subclass can override an inherited method: give it the same signature, a new body.
Dog'sspeak()replacesAnimal'sspeak()forDogobjects.- Methods it does not override (like
getName()) are still inherited unchanged.
การแทนที่เมทอด
- subclass สามารถ override เมทอดที่ถูกสืบทอด: ให้它有 signature เดิม พร้อม body ใหม่
Dog'sspeak()แทนที่Animal'sspeak()สำหรับDogobjects- เมทอดที่มันไม่ override (เช่น
getName()) ยังคงถูกสืบทอดโดยไม่เปลี่ยนแปลง
super.method() — extend, don't replace
- Sometimes you want the parent's work plus a little more.
- Inside an override,
super.describe()runs the superclass version of the method. - Then you add to its result instead of rewriting it from scratch.
super.method() — ขยาย ไม่ใช่แทนที่
- บางครั้งคุณต้องการงานของ parent บวก กับอีกเล็กน้อย
- ภายใน override,
super.describe()รัน superclass version ของメソッド - จากนั้นคุณเพิ่มเข้าไปในผลลัพธ์ของมันแทนที่จะเขียนใหม่ทั้งหมดจากศูนย์
public class Main {
public static void main(String[] args) {
Bird b = new Bird("Tweety");
System.out.println(b.describe());
// I am an animal named Tweety, and I can fly
}
}
class Animal {
private String name;
public Animal(String name) { this.name = name; }
public String describe() { return "I am an animal named " + this.name; }
}
class Bird extends Animal {
public Bird(String name) { super(name); }
public String describe() {
return super.describe() + ", and I can fly"; // reuse, then add
}
}
is-a: a subclass fits where its superclass is expected
- Because a
Dogis anAnimal, you can writeAnimal a = new Dog("Spot");. - The variable's type is
Animal, but the object is really aDog. - This "is-a" rule is what makes the next lesson — polymorphism — possible.
is-a: subclass เข้าได้เมื่อ انتظارของ它的 superclass
- เพราะ一个
Dogเป็น-anAnimal, คุณสามารถเขียนAnimal a = new Dog("Spot"); - type ของตัวแปรคือ
Animalแต่ object จริงๆ เป็นDog - กฎ "is-a" นี้คือสิ่งที่ทำให้บทเรียนถัดไป — polymorphism — เป็นไปได้
Common mistakes
super(...)must be the first line of a subclass constructor.- Add
@Overrideso the compiler catches a wrong signature.
ข้อผิดพลาดที่พบบ่อย
super(...)ต้องเป็นบรรทัดแรกของ subclass constructor- เพิ่ม
@Overrideเพื่อให้ compiler จับ signature ผิดพลาดได้
Now you try
- Each task gives you a finished
Animalsuperclass. Write or complete the subclass below it. - A hidden Harness builds your object and checks both inherited and new behaviour.
- Press Run to compile, then Check answer. Your code runs on the server, so even the first run is fast.
ลองดูเลย
- แต่ละงานให้คุณมี superclass
Animalที่สมบูรณ์แล้ว เขียนหรือเติม subclass ด้านล่างมัน - Harness ที่ซ่อนอยู่สร้าง object ของคุณและตรวจสอบทั้งพฤติกรรมที่ถูกสืบทอดและใหม่
- กด Run เพื่อคอมไพล์ แล้ว Check answer โค้ดของคุณรันบน server ดังนั้นแม้แต่ครั้งแรกก็เร็ว
Below the finished Animal, write a class Cat that extends Animal. Give it a constructor Cat(String name) that passes the name up with super(name). Add nothing else — Cat should inherit getName() for free. · ใต้ Animal ที่เสร็จสมบูรณ์แล้ว ให้สร้างคลาส Cat ที่ extends Animal มี constructor Cat(String name) ที่ส่งชื่อขึ้นไปด้วย super(name) เพิ่มสิ่งอื่นไม่ได้ — Cat จะสืบทอด getName() มาได้โดยอัตโนมัติ
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete the class Cow (it already extends Animal). Override speak() so a Cow returns "Moo" instead of the default. getName() must still work (inherited). · เติมเต็มคลาส Cow (ซึ่ง extends Animal อยู่แล้ว) Override speak() เพื่อให้ Cow กลับค่า "Moo" แทนค่าเริ่มต้น getName() ต้องยังทำงานได้ (สืบทอดมา)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete the class Fish (it already extends Animal). Override describe() so it returns the Animal description plus ", and I can swim". Reuse the parent's work by calling super.describe() — do not rewrite the whole sentence. · เติมเต็มคลาส Fish (ซึ่ง extends Animal อยู่แล้ว) Override describe() เพื่อกลับค่าคำอธิบาย Animal เพิ่ม ", and I can swim" เข้าไป ใช้งานของ parent โดยเรียก super.describe() — อย่าเขียนประโยคใหม่ทั้งเส้น
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่