חלקPart 4: OOP מתקדםAdvanced OOP
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
ירושה, super ו-overrideInheritance, super & override
extends, super() ו-super.method(), @Override, ומתי ירושה היא רעיון רע.
extends, super() and super.method(), @Override, and when inheritance is a bad idea.
הנקודות החשובותKey points
- ירושה:
class EBook extends Book:inheritance:class EBook extends Book: - מחלקת על (הורה): המחלקה שממנה יורשים.superclass (parent): The class you inherit from.
EBookיורשת מ-Book: יש להgetTitleבלי לכתוב אותה.EBookinherits fromBook: it hasgetTitlewithout writing it.- הבנאי שלה קורא קודם ל-
super(title).Its constructor first callssuper(title). - אם בנאי לא קורא ל-
super(...), Java מוסיפהsuper()ריק.If a constructor doesn't callsuper(...), Java adds an emptysuper(). - ל-
Bookאין בנאי ריק, ולכן javac עוצר.Bookhas no empty constructor, so javac stops. Stack extends ArrayListנראה חסכוני, אבל עכשיו כל מי שמחזיקStackיכול לקרוא ל-add(0, 99)ולשבור את הכלל "האחרון שנכנס יוצא ראשון".Stack extends ArrayListlooks economical, but now anyone holding aStackcan calladd(0, 99)and break the rule "last in, first out".- מחסנית היא לא סוג של רשימה, היא משתמשת ברשימה.A stack isn't a kind of list, it uses a list.
constructor Book in class Book cannot be applied to given types;:constructor Book in class Book cannot be applied to given types;:
מונחיםTerms
- ירושהinheritance:
class EBook extends Book: ל-EBook יש את כל השדות והמתודות של Book, והיא יכולה להוסיף ולשנות. לכל מחלקה הורה אחד בלבד.class EBook extends Book: EBook has every field and method of Book, and can add and change. Each class has exactly one parent. - מחלקת על (הורה)superclass (parent): המחלקה שממנה יורשים. מחלקה שלא כתוב לה
extendsיורשת מ-Object.The class you inherit from. A class withoutextendsinherits fromObject. - supersuper:
super(...)בשורה הראשונה של בנאי קורא לבנאי של ההורה.super.describe()קורא לגרסה של ההורה מתוך מתודה שדורסת אותה.super(...)on a constructor's first line calls the parent's constructor.super.describe()calls the parent's version from inside a method that overrides it. - דריסהoverriding: מתודה בתת-מחלקה עם אותה חתימה בדיוק כמו בהורה, שמחליפה את ההתנהגות.
@Overrideמבקש מ-javac לוודא שזו באמת דריסה.A method in a subclass with exactly the parent's signature, replacing the behaviour.@Overrideasks javac to make sure it really is an override. - protectedprotected: גישה למחלקה, לתת-המחלקות שלה ולמחלקות באותה חבילה. נוח, אבל חושף מימוש לכל מי שיורש.Access for the class, its subclasses and classes in the same package. Handy, but it exposes the implementation to every subclass.
- is-a מול has-ais-a vs has-a: ירושה מתאימה כשמשפט "X הוא סוג של Y" נכון תמיד (EBook הוא ספר). כש-X רק משתמש ב-Y, עדיף הרכבה (has-a): שדה מסוג Y.Inheritance fits when "X is a kind of Y" is always true (an EBook is a book). When X only uses Y, prefer composition (has-a): a field of type Y.
- ירושהinheritance:
class EBook extends Book: ל-EBook יש את כל השדות והמתודות של Book, והיא יכולה להוסיף ולשנות. לכל מחלקה הורה אחד בלבד.class EBook extends Book: EBook has every field and method of Book, and can add and change. Each class has exactly one parent. - מחלקת על (הורה)superclass (parent): המחלקה שממנה יורשים. מחלקה שלא כתוב לה
extendsיורשת מ-Object.The class you inherit from. A class withoutextendsinherits fromObject. - supersuper:
super(...)בשורה הראשונה של בנאי קורא לבנאי של ההורה.super.describe()קורא לגרסה של ההורה מתוך מתודה שדורסת אותה.super(...)on a constructor's first line calls the parent's constructor.super.describe()calls the parent's version from inside a method that overrides it. - דריסהoverriding: מתודה בתת-מחלקה עם אותה חתימה בדיוק כמו בהורה, שמחליפה את ההתנהגות.
@Overrideמבקש מ-javac לוודא שזו באמת דריסה.A method in a subclass with exactly the parent's signature, replacing the behaviour.@Overrideasks javac to make sure it really is an override. - protectedprotected: גישה למחלקה, לתת-המחלקות שלה ולמחלקות באותה חבילה. נוח, אבל חושף מימוש לכל מי שיורש.Access for the class, its subclasses and classes in the same package. Handy, but it exposes the implementation to every subclass.
- is-a מול has-ais-a vs has-a: ירושה מתאימה כשמשפט "X הוא סוג של Y" נכון תמיד (EBook הוא ספר). כש-X רק משתמש ב-Y, עדיף הרכבה (has-a): שדה מסוג Y.Inheritance fits when "X is a kind of Y" is always true (an EBook is a book). When X only uses Y, prefer composition (has-a): a field of type Y.
Cheatsheet
extends, super ו-@Overrideextends, super and @Override
Book b = new Book("Emma");
EBook e = new EBook("Dune", 2);
System.out.println(b.describe());
System.out.println(e.describe());
System.out.println(e.getTitle());כשירושה היא רעיון רעWhen inheritance is a bad idea
Stack s = new Stack();
s.push(1);
s.push(2);
s.add(0, 99);
System.out.println(s.pop() + " " + s.pop() + " " + s.pop());שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|---|---|
constructor Book in class Book cannot be applied to given ty | כששוכחים את super(...)When you forget super(...) | constructor Book in class Book cannot be applied to given types;: ה-super() שנוסף לבד לא מתאים ל-Book(String).constructor Book in class Book cannot be applied to given types;: the super() added for you doesn't match Book(String). |
constructor Book in class Book cannot be applied to given ty | צייד באגים: super חסרBug hunter: a missing super | constructor Book in class Book cannot be applied to given types;: הבנאי של EBook לא קרא ל-super, ו-Java ניסתה super() ריק שלא קיים.constructor Book in class Book cannot be applied to given types;: EBook's constructor didn't call super, so Java tried an empty super() that doesn't exist. |