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

חזרה למפגשBack
JavaJava

ירושה, super ו-overrideInheritance, super & override

מפגש 19: extends, super() ו-super.method(), @Override, ומתי ירושה היא רעיון רע.Session 19: extends, super() and super.method(), @Override, and when inheritance is a bad idea.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • extends, super ו-@Overrideextends, super and @Override
  • כששוכחים את super(...)When you forget super(...)
  • כשירושה היא רעיון רעWhen inheritance is a bad idea
  • טעויות נפוצותCommon mistakes
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • ירושה: class EBook extends Book:inheritance: class EBook extends Book:
  • מחלקת על (הורה): המחלקה שממנה יורשים.superclass (parent): The class you inherit from.
  • super: super(...) בשורה הראשונה של בנאי קורא לבנאי של ההורה.super: super(...) on a constructor's first line calls the parent's constructor.
  • דריסה: מתודה בתת-מחלקה עם אותה חתימה בדיוק כמו בהורה, שמחליפה את ההתנהגות.overriding: A method in a subclass with exactly the parent's signature, replacing the behaviour.
  • protected: גישה למחלקה, לתת-המחלקות שלה ולמחלקות באותה חבילה.protected: Access for the class, its subclasses and classes in the same package.
  • is-a מול has-a: ירושה מתאימה כשמשפט "X הוא סוג של Y" נכון תמיד (EBook הוא ספר).is-a vs has-a: Inheritance fits when "X is a kind of Y" is always true (an EBook is a book).
נושאTopic 2

extends, super ו-@Overrideextends, super and @Override

extends, super ו-@Overrideextends, super and @Override

  • EBook יורשת מ-Book: יש לה getTitle בלי לכתוב אותה.EBook inherits from Book: it has getTitle without writing it.
  • הבנאי שלה קורא קודם ל-super(title).Its constructor first calls super(title).
  • describe נדרסת, ומשתמשת בגרסה של ההורה עם super.describe().describe is overridden, and uses the parent's version with super.describe().
נושאTopic 3

כששוכחים את super(...)When you forget super(...)

כששוכחים את super(...)When you forget super(...)

  • אם בנאי לא קורא ל-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.
נושאTopic 4

כשירושה היא רעיון רעWhen inheritance is a bad idea

כשירושה היא רעיון רעWhen inheritance is a bad idea

  • 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.
  • באתגר 1 תתקנו את זה עם הרכבה.In challenge 1 you'll fix it with composition (has-a).
נושאTopic 5

טעויות נפוצותCommon mistakes

טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says

  • constructor Book in class Book cannot be applied to given types;:constructor Book in class Book cannot be applied to given types;:
נושאTopic 6

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • מדף, צעד 19: EBook ו-AudioBookShelf, step 19: EBook and AudioBook

ירושה: class EBook extends Book:inheritance: class EBook extends Book:

מחלקת על (הורה): המחלקה שממנה יורשים.superclass (parent): The class you inherit from.

super: super(...) בשורה הראשונה של בנאי קורא לבנאי של ההורה.super: super(...) on a constructor's first line calls the parent's constructor.

דריסה: מתודה בתת-מחלקה עם אותה חתימה בדיוק כמו בהורה, שמחליפה את ההתנהגות.overriding: A method in a subclass with exactly the parent's signature, replacing the behaviour.

protected: גישה למחלקה, לתת-המחלקות שלה ולמחלקות באותה חבילה.protected: Access for the class, its subclasses and classes in the same package.

is-a מול has-a: ירושה מתאימה כשמשפט "X הוא סוג של Y" נכון תמיד (EBook הוא ספר).is-a vs has-a: Inheritance fits when "X is a kind of Y" is always true (an EBook is a book).

EBook יורשת מ-Book: יש לה getTitle בלי לכתוב אותה.EBook inherits from Book: it has getTitle without writing it.

הבנאי שלה קורא קודם ל-super(title).Its constructor first calls super(title).

describe נדרסת, ומשתמשת בגרסה של ההורה עם super.describe().describe is overridden, and uses the parent's version with super.describe().

אם בנאי לא קורא ל-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.

באתגר 1 תתקנו את זה עם הרכבה.In challenge 1 you'll fix it with composition (has-a).

constructor Book in class Book cannot be applied to given types;:constructor Book in class Book cannot be applied to given types;:

מדף, צעד 19: EBook ו-AudioBookShelf, step 19: EBook and AudioBook

class EBook extends Book. מה נכון?class EBook extends Book. What's true?

  • EBook מקבלת את המתודות של BookEBook gets Book's methods
  • Book מקבלת את המתודות של EBookBook gets EBook's methods
  • EBook יכולה לרשת גם ממחלקה נוספתEBook can also extend another class
  • צריך להעתיק את השדות של BookYou must copy Book's fields

EBook מקבלת את המתודות של BookEBook gets Book's methods

מה מדפיס new C() כש-C יורשת מ-B שיורשת מ-A, וכל בנאי מדפיס את שמו?What does new C() print when C extends B extends A, and each constructor prints its name?

  • C B AC B A
  • A B CA B C
  • CC
  • AA

A B CA B C

מה עושה @Override?What does @Override do?

  • גורם למתודה לדרוסMakes the method override
  • מבקש מ-javac לעצור אם זו לא דריסה אמיתיתAsks javac to stop if it's not a real override
  • הופך את המתודה ל-finalMakes the method final
  • שום דבר, זו הערהNothing, it's a comment

מבקש מ-javac לעצור אם זו לא דריסה אמיתיתAsks javac to stop if it's not a real override

חיות מדברותTalking animals

  • ל-Animal יש name, מתודה sound() שמחזירה ..., ו-describe() שמחזירה Rex says Woof.Animal has a name, a method sound() returning ..., and describe() returning Rex says Woof.

מנהל עם בונוסA manager with a bonus

  • Manager extends Employee ומוסיפה bonus.Manager extends Employee and adds a bonus.

צייד באגים: super חסרBug hunter: a missing super

  • התוכנית מהתזכורת לא מתקמפלת.The program from the refresher doesn't compile.

צייד באגים: הדריסה שלא קרתהBug hunter: the override that never happened

  • התוכנית מדפיסה `Book:The program prints `Book:

מי נבנה קודם?Who's built first?

  • שלוש מחלקות:Three classes:

מדף, צעד 19: EBook ו-AudioBookShelf, step 19: EBook and AudioBook

  • Book(title, author) עם describe() (Emma by Austen).Book(title, author) with describe() (Emma by Austen).

מחסנית עם הרכבהA stack with composition

  • כתבו מחדש את Stack מהתזכורת בלי ירושה:Rewrite Stack from the refresher without inheritance:

חיסכון עם מינימוםSavings with a minimum

  • ל-Account יש protected int balance ו-withdraw.Account has protected int balance and withdraw.

מה למדנוWhat we learned

  • ירושה: class EBook extends Book:inheritance: class EBook extends Book:
  • EBook יורשת מ-Book: יש לה getTitle בלי לכתוב אותה.EBook inherits from Book: it has getTitle without writing it.
  • אם בנאי לא קורא ל-super(...), Java מוסיפה super() ריק.If a constructor doesn't call super(...), Java adds an empty super().
  • 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".
  • constructor Book in class Book cannot be applied to given types;:constructor Book in class Book cannot be applied to given types;:
  • מדף, צעד 19: EBook ו-AudioBookShelf, step 19: EBook and AudioBook

עכשיו לתרגולNow, practice

6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

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