Inheritance: extends, super, and overriding · الوراثة: 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).
- الفئة الابنة هي نوع من فئتها الأب:
Dogهو نوع منAnimal.
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فئة ابنة لـAnimal. Dogيحصل تلقائياً على كل طريقة عامة منAnimal، مثلgetName().- مُنشئ الفئة الابنة يُستدعي مُنشئ الفئة الأب أولاً مع
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.
إعادة التعريف لطريقة
- يمكن للفئة الابنة إعادة تعريف طريقة موروثة: إعطاؤها نفس التوقيع، جسماً جديداً.
Dog'sspeak()يحل محلAnimal'sspeak()لـDogكائنات.- الطرق التي لا تقوم بإعادة تعريفها (مثل
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() — امتد، لا استبدل
- أحياناً تريد عمل الأب زائد القليل من المزيد.
- داخل إعادة التعريف،
super.describe()يُنفذ نسخة الفئة الأب للطريقة. - ثم تضيف إلى نتيجتها بدلاً من إعادة كتابتها من الصفر.
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: الفئة الابنة تتناسب حيث تُتوقع فئتها الأب
- لأن
Dogهو نوع منAnimal، يمكنك كتابةAnimal a = new Dog("Spot");. - نوع المتغير هو
Animal، لكن الكائن فعلياً هوDog. - قاعدة "is-a" هذه هي ما يجعل الدرس القادم — تعدد الأشكال — ممكناً.
Common mistakes
super(...)must be the first line of a subclass constructor.- Add
@Overrideso the compiler catches a wrong signature.
أخطاء شائعة
super(...)يجب أن يكون السطر الأول من مُنشئ الفئة الابنة.- أضف
@Overrideحتى يلتفت المترجم إلى توقيع خاطئ.
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.
الآن جرب بنفسك
- كل مهمة تعطيك فئة أب
Animalمكتملة. اكتب أو أكمل الفئة الابنة أسفلها. - مختبر مخفي يبني كائنك ويتحقق من السلوك الموروث والجديد.
- اضغط Run للمترجمة، ثم Check answer. يعمل كودك على الخادم، لذا حتى أول تشغيل سريع.
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 تمتد Animal. أعطها بناءً Cat(String name) يمرر الاسم للأعلى مع super(name). لا تضيف شيئاً آخر — Cat ستورث getName() مجاناً.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
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 (هي تمتد بالفعل من Animal). عدّل speak() بحيث تُرجع Cow قيمة "Moo" بدلاً من الافتراضية. يجب أن تعمل getName() أيضاً (موروثة).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
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 (هي تمتد بالفعل من Animal). عدّل describe() لتُرجع وصف Animal بالإضافة إلى ", and I can swim". أعد استخدام عمل الأب باستدعاء super.describe() — لا تعيد كتابة الجملة كاملة.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.