נבנה על ידיBuilt by Ariel Yohimovich

JavaJava
19
חלק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.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • ירושה: class EBook extends Book:inheritance: class EBook extends Book:
  • מחלקת על (הורה): המחלקה שממנה יורשים.superclass (parent): The class you inherit from.
  • EBook יורשת מ-Book: יש לה getTitle בלי לכתוב אותה.EBook inherits from Book: it has getTitle without writing it.
  • הבנאי שלה קורא קודם ל-super(title).Its constructor first calls super(title).
  • אם בנאי לא קורא ל-super(...), Java מוסיפה super() ריק.If a constructor doesn't call super(...), Java adds an empty super().
  • ל-Book אין בנאי ריק, ולכן javac עוצר.Book has no empty constructor, so javac stops.
  • Stack extends ArrayList נראה חסכוני, אבל עכשיו כל מי שמחזיק Stack יכול לקרוא ל-add(0, 99) ולשבור את הכלל "האחרון שנכנס יוצא ראשון".Stack extends ArrayList looks economical, but now anyone holding a Stack can call add(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 without extends inherits from Object.
  • 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. @Override asks 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 without extends inherits from Object.
  • 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. @Override asks 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 superconstructor 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.

נבנה על ידיBuilt by Ariel Yohimovich