נבנה על ידיBuilt by Ariel Yohimovich
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.
extends, super() ו-super.method(), @Override, ומתי ירושה היא רעיון רע. בסוף המפגש מוסיפים צעד לפרויקט המלווה.extends, super() and super.method(), @Override, and when inheritance is a bad idea. At the end we add a step to the companion project.מה נעשה היום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
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגש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).
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושא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().
EBook יורשת מ-Book: יש לה getTitle בלי לכתוב אותה. הבנאי שלה קורא קודם ל-super(title). describe נדרסת, ומשתמשת בגרסה של ההורה עם super.describe(). בזיכרון רואים עצם EBook אחד שמחזיק גם את title של ההורה.EBook inherits from Book: it has getTitle without writing it. Its constructor first calls super(title). describe is overridden, and uses the parent's version with super.describe(). In memory you see one EBook object that holds the parent's title too.נושא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.
אם בנאי לא קורא ל-super(...), Java מוסיפה super() ריק. ל-Book אין בנאי ריק, ולכן javac עוצר.If a constructor doesn't call super(...), Java adds an empty super(). 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).
Stack extends ArrayList נראה חסכוני, אבל עכשיו כל מי שמחזיק Stack יכול לקרוא ל-add(0, 99) ולשבור את הכלל "האחרון שנכנס יוצא ראשון". מחסנית היא לא סוג של רשימה, היא משתמשת ברשימה. באתגר 1 תתקנו את זה עם הרכבה.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. 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;:
אלה הטעויות שהתרגילים מהסוג "צייד באגים" בנויים סביבן. כל אחת רצה באמת, והפלט הוא מה ש-Java עונה.These are the mistakes the "bug hunter" exercises are built around. Each one really runs, and the output is what Java says.נושאTopic 6
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
- מדף, צעד 19: EBook ו-AudioBookShelf, step 19: EBook and AudioBook
Book(title, author) עם describe() (Emma by Austen). EBook מוסיפה גודל (Dune by Herbert [ebook, 2 MB]), ו-AudioBook מוסיפה דקות (Ulysses by Joyce [audio, 1h 30m]). שתיהן משתמשות ב-super.describe(). הדפיסו את שלושתם.Book(title, author) with describe() (Emma by Austen). EBook adds a size (Dune by Herbert [ebook, 2 MB]), and AudioBook adds minutes (Ulysses by Joyce [audio, 1h 30m]). Both use super.describe(). Print all three.ירושה: class EBook extends Book:inheritance: class EBook extends Book:
המונחים של המפגשThis session's termsמחלקת על (הורה): המחלקה שממנה יורשים.superclass (parent): The class you inherit from.
המונחים של המפגשThis session's termssuper: super(...) בשורה הראשונה של בנאי קורא לבנאי של ההורה.super: super(...) on a constructor's first line calls the parent's constructor.
המונחים של המפגשThis session's termsדריסה: מתודה בתת-מחלקה עם אותה חתימה בדיוק כמו בהורה, שמחליפה את ההתנהגות.overriding: A method in a subclass with exactly the parent's signature, replacing the behaviour.
המונחים של המפגשThis session's termsprotected: גישה למחלקה, לתת-המחלקות שלה ולמחלקות באותה חבילה.protected: Access for the class, its subclasses and classes in the same package.
המונחים של המפגשThis session's termsis-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).
המונחים של המפגשThis session's termsEBook יורשת מ-Book: יש לה getTitle בלי לכתוב אותה.EBook inherits from Book: it has getTitle without writing it.
extends, super ו-@Overrideextends, super and @Overrideהבנאי שלה קורא קודם ל-super(title).Its constructor first calls super(title).
extends, super ו-@Overrideextends, super and @Overridedescribe נדרסת, ומשתמשת בגרסה של ההורה עם super.describe().describe is overridden, and uses the parent's version with super.describe().
extends, super ו-@Overrideextends, super and @Overrideאם בנאי לא קורא ל-super(...), Java מוסיפה super() ריק.If a constructor doesn't call super(...), Java adds an empty super().
כששוכחים את super(...)When you forget super(...)ל-Book אין בנאי ריק, ולכן javac עוצר.Book has no empty constructor, so javac stops.
כששוכחים את super(...)When you forget 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".
כשירושה היא רעיון רעWhen inheritance is a bad ideaמחסנית היא לא סוג של רשימה, היא משתמשת ברשימה.A stack isn't a kind of list, it uses a list.
כשירושה היא רעיון רעWhen inheritance is a bad ideaבאתגר 1 תתקנו את זה עם הרכבה.In challenge 1 you'll fix it with composition (has-a).
כשירושה היא רעיון רעWhen inheritance is a bad ideaconstructor Book in class Book cannot be applied to given types;:constructor Book in class Book cannot be applied to given types;:
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java saysמדף, צעד 19: EBook ו-AudioBookShelf, step 19: EBook and AudioBook
הצעד של היום בפרויקט מדףToday's step in the Shelf projectclass 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
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.EBook מקבלת את המתודות של BookEBook gets Book's methods
נכון, וגם יכולה לדרוס אותן.Right, and it can override them.מה מדפיס new C() כש-C יורשת מ-B שיורשת מ-A, וכל בנאי מדפיס את שמו?What does new C() print when C extends B extends A, and each constructor prints its name?
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.A B CA B C
נכון: כל בנאי קורא קודם להורה שלו.Right: every constructor calls its parent first.מה עושה @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
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.מבקש מ-javac לעצור אם זו לא דריסה אמיתיתAsks javac to stop if it's not a real override
נכון: תופס שגיאות כתיב ופרמטרים שגויים.Right: it catches typos and wrong parameters.חיות מדברותTalking animals
- ל-
Animal יש name, מתודה sound() שמחזירה ..., ו-describe() שמחזירה Rex says Woof.Animal has a name, a method sound() returning ..., and describe() returning Rex says Woof.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מנהל עם בונוסA manager with a bonus
Manager extends Employee ומוסיפה bonus.Manager extends Employee and adds a bonus.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: super חסרBug hunter: a missing super
- התוכנית מהתזכורת לא מתקמפלת.The program from the refresher doesn't compile.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: הדריסה שלא קרתהBug hunter: the override that never happened
- התוכנית מדפיסה `Book:The program prints `Book:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מי נבנה קודם?Who's built first?
- שלוש מחלקות:Three classes:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 19: EBook ו-AudioBookShelf, step 19: EBook and AudioBook
Book(title, author) עם describe() (Emma by Austen).Book(title, author) with describe() (Emma by Austen).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מחסנית עם הרכבהA stack with composition
- כתבו מחדש את
Stack מהתזכורת בלי ירושה:Rewrite Stack from the refresher without inheritance:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.חיסכון עם מינימוםSavings with a minimum
- ל-
Account יש protected int balance ו-withdraw.Account has protected int balance and withdraw.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מה למדנו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
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.