ירושה, super ו-overrideInheritance, super & override
extends, super() ו-super.method(), @Override, ומתי ירושה היא רעיון רע.
extends, super() and super.method(), @Override, and when inheritance is a bad idea.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
חיות מדברותTalking animals
ל-Animal יש name, מתודה sound() שמחזירה ..., ו-describe() שמחזירה Rex says Woof. כתבו Dog ו-Cat שיורשות ודורסות רק את sound. הדפיסו Rex says Woof ו-Mitzi says Meow.
Animal has a name, a method sound() returning ..., and describe() returning Rex says Woof. Write Dog and Cat that extend it and override only sound. Print Rex says Woof and Mitzi says Meow.
איך הפלט אמור להיראות?What should the output look like?
Rex says Woof
Mitzi says Meow
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:describe נשארת בהורה, ו-sound() שבתוכה מגיעה מהתת-מחלקה.describe stays in the parent, and the sound() inside it comes from the subclass.
Manager extends Employee ומוסיפה bonus. דרסו את salary() כך שתחזיר את המשכורת של ההורה ועוד הבונוס, בלי לגשת ל-base (הוא private). הדפיסו Noa 10000 ו-Avi 15000.
Manager extends Employee and adds a bonus. Override salary() to return the parent's salary plus the bonus, without touching base (it's private). Print Noa 10000 and Avi 15000.
איך הפלט אמור להיראות?What should the output look like?
Noa 10000
Avi 15000
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
התוכנית מהתזכורת לא מתקמפלת. תקנו את הבנאי של EBook כך שיקרא ל-super("Untitled"), ויודפס Untitled.
The program from the refresher doesn't compile. Fix EBook's constructor to call super("Untitled"), so it prints Untitled.
איך הפלט אמור להיראות?What should the output look like?
Untitled
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:super(...) חייב להיות השורה הראשונה בבנאי.super(...) must be the constructor's first line.
צייד באגים: הדריסה שלא קרתהBug hunter: the override that never happened
התוכנית מדפיסה Book: Dune במקום EBook: Dune (2 MB). הוסיפו @Override מעל המתודה ב-EBook, קראו מה javac אומר, ותקנו.
The program prints Book: Dune instead of EBook: Dune (2 MB). Add @Override above the method in EBook, read what javac says, and fix it.
איך הפלט אמור להיראות?What should the output look like?
EBook: Dune (2 MB)
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:בדקו את האיות של שם המתודה.Check the spelling of the method's name.
רמזHint 2:עם @Override javac עונה method does not override or implement a method from a supertype.With @Override javac says method does not override or implement a method from a supertype.
שלוש מחלקות: C extends B, B extends A. כל בנאי מדפיס את השם שלו עם print. new C() מדפיס A B C. כתבו וצפו בסימולציה.
Three classes: C extends B, B extends A. Each constructor prints its name with print. new C() prints A B C. Write it and watch the simulation.
איך הפלט אמור להיראות?What should the output look like?
A B C
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:בכל בנאי יש super() נסתר בשורה הראשונה.Every constructor has a hidden super() on its first line.
מדף, צעד 19: EBook ו-AudioBookShelf, step 19: EBook and AudioBook
פרויקט מלווהCompanion project
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.
איך הפלט אמור להיראות?What should the output look like?
Emma by Austen
Dune by Herbert [ebook, 2 MB]
Ulysses by Joyce [audio, 1h 30m]
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
כתבו מחדש את Stack מהתזכורת בלי ירושה: שדה private final List<Integer> items, ורק push, pop, peek ו-size. דחפו 1, 2, 3 והדפיסו 3 2 2 (pop, peek, size).
Rewrite Stack from the refresher without inheritance: a field private final List<Integer> items, and only push, pop, peek and size. Push 1, 2, 3 and print 3 2 2 (pop, peek, size).
איך הפלט אמור להיראות?What should the output look like?
3 2 2
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:עכשיו ל-Stack אין add: אי אפשר לשבור אותה מבחוץ.Now Stack has no add: nobody can break it from outside.
ל-Account יש protected int balance ו-withdraw. Savings דורסת את withdraw כך שהיתרה לא תרד מתחת ל-MINIMUM = 100 (ואז קוראת ל-super.withdraw), ומוסיפה addInterest() של 10%. מ-500: משכו 300 (true 200), ואז 150 (false 200), ואז ריבית (220).
Account has protected int balance and withdraw. Savings overrides withdraw so the balance never goes below MINIMUM = 100 (then calls super.withdraw), and adds addInterest() of 10%. From 500: withdraw 300 (true 200), then 150 (false 200), then interest (220).
איך הפלט אמור להיראות?What should the output look like?
true 200
false 200
220
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:protected מאפשר ל-Savings לגשת ל-balance.protected lets Savings reach balance.