נבנה על ידיBuilt by Ariel Yohimovich
JavaJava
abstract ו-interfacesabstract & interfaces
מפגש 20: מחלקות מופשטות, interfaces עם default ו-static methods, ומימוש של כמה interfaces.Session 20: Abstract classes, interfaces with default and static methods, and implementing several interfaces.
מחלקות מופשטות, interfaces עם default ו-static methods, ומימוש של כמה interfaces. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Abstract classes, interfaces with default and static methods, and implementing several interfaces. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- מחלקה מופשטת: תבנית חלקיתAn abstract class: a partial blueprint
- אי אפשר ליצור עצם מופשטYou can't create an abstract object
- interface, default, וכמה interfacesinterface, default, and several interfaces
- טעויות נפוצותCommon mistakes
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגשThis session's terms
- מחלקה מופשטת:
abstract class Shape:abstract class: abstract class Shape: - מתודה מופשטת:
abstract double area();:abstract method: abstract double area();: - interface: חוזה:interface: A contract:
- מתודת default: מתודה עם גוף בתוך interface.default method: A method with a body inside an interface.
- Comparable:
Comparable<T> מגדיר "סדר טבעי":Comparable: Comparable<T> defines a "natural order": - Template Method: מתודה בהורה (לרוב
final) שקובעת את סדר השלבים, ומשאירה שלבים מופשטים לתת-המחלקות.Template Method: A method in the parent (often final) that fixes the order of the steps, leaving abstract steps to the subclasses.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושאTopic 2
מחלקה מופשטת: תבנית חלקיתAn abstract class: a partial blueprint
מחלקה מופשטת: תבנית חלקיתAn abstract class: a partial blueprint
- ל-
Shape אין דרך לחשב שטח, ולכן area מופשטת.Shape has no way to compute an area, so area is abstract. describe כתובה פעם אחת בהורה, ובזמן ריצה קוראת ל-area של העצם האמיתי (עיגול או ריבוע).describe is written once in the parent, and at run time it calls the real object's area (circle or square).
ל-Shape אין דרך לחשב שטח, ולכן area מופשטת. describe כתובה פעם אחת בהורה, ובזמן ריצה קוראת ל-area של העצם האמיתי (עיגול או ריבוע).Shape has no way to compute an area, so area is abstract. describe is written once in the parent, and at run time it calls the real object's area (circle or square).נושאTopic 3
אי אפשר ליצור עצם מופשטYou can't create an abstract object
אי אפשר ליצור עצם מופשטYou can't create an abstract object
new Shape() לא מתקמפל: מה היה השטח שלו?new Shape() doesn't compile: what would its area be?
new Shape() לא מתקמפל: מה היה השטח שלו?new Shape() doesn't compile: what would its area be?נושאTopic 4
interface, default, וכמה interfacesinterface, default, and several interfaces
interface, default, וכמה interfacesinterface, default, and several interfaces
Duck מממשת שני interfaces.Duck implements two interfaces.- מ-
Flyer היא מקבלת את describe (מתודת default) בחינם.From Flyer it gets describe (a default method) for free. - משתנה מסוג
Flyer רואה רק את מה ש-Flyer מבטיח.A variable of type Flyer sees only what Flyer promises.
Duck מממשת שני interfaces. מ-Flyer היא מקבלת את describe (מתודת default) בחינם. משתנה מסוג Flyer רואה רק את מה ש-Flyer מבטיח. מתודות שמממשות interface חייבות להיות public.Duck implements two interfaces. From Flyer it gets describe (a default method) for free. A variable of type Flyer sees only what Flyer promises. Methods implementing an interface must be public.נושאTopic 5
טעויות נפוצותCommon mistakes
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says
Circle is not abstract and does not override abstract method area() in Shape:Circle is not abstract and does not override abstract method area() in Shape:
אלה הטעויות שהתרגילים מהסוג "צייד באגים" בנויים סביבן. כל אחת רצה באמת, והפלט הוא מה ש-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
- מדף, צעד 20: interface בשם LendableShelf, step 20: a Lendable interface
interface Lendable עם name(), borrower(), lendTo, giveBack ו-default String status() (Dune: lent to Noa או DVD Alien: available). Book ו-Dvd מממשות. השאילו את Dune ל-Noa, הדפיסו את המצב של שניהם, החזירו, והדפיסו שוב את Dune.interface Lendable with name(), borrower(), lendTo, giveBack and default String status() (Dune: lent to Noa or DVD Alien: available). Book and Dvd implement it. Lend Dune to Noa, print both statuses, give it back, and print Dune again.מחלקה מופשטת: abstract class Shape:abstract class: abstract class Shape:
המונחים של המפגשThis session's termsמתודה מופשטת: abstract double area();:abstract method: abstract double area();:
המונחים של המפגשThis session's termsinterface: חוזה:interface: A contract:
המונחים של המפגשThis session's termsמתודת default: מתודה עם גוף בתוך interface.default method: A method with a body inside an interface.
המונחים של המפגשThis session's termsComparable: Comparable<T> מגדיר "סדר טבעי":Comparable: Comparable<T> defines a "natural order":
המונחים של המפגשThis session's termsTemplate Method: מתודה בהורה (לרוב final) שקובעת את סדר השלבים, ומשאירה שלבים מופשטים לתת-המחלקות.Template Method: A method in the parent (often final) that fixes the order of the steps, leaving abstract steps to the subclasses.
המונחים של המפגשThis session's termsל-Shape אין דרך לחשב שטח, ולכן area מופשטת.Shape has no way to compute an area, so area is abstract.
מחלקה מופשטת: תבנית חלקיתAn abstract class: a partial blueprintdescribe כתובה פעם אחת בהורה, ובזמן ריצה קוראת ל-area של העצם האמיתי (עיגול או ריבוע).describe is written once in the parent, and at run time it calls the real object's area (circle or square).
מחלקה מופשטת: תבנית חלקיתAn abstract class: a partial blueprintnew Shape() לא מתקמפל: מה היה השטח שלו?new Shape() doesn't compile: what would its area be?
אי אפשר ליצור עצם מופשטYou can't create an abstract objectinterface, default, וכמה interfacesinterface, default, and several interfacesמ-Flyer היא מקבלת את describe (מתודת default) בחינם.From Flyer it gets describe (a default method) for free.
interface, default, וכמה interfacesinterface, default, and several interfacesמשתנה מסוג Flyer רואה רק את מה ש-Flyer מבטיח.A variable of type Flyer sees only what Flyer promises.
interface, default, וכמה interfacesinterface, default, and several interfacesCircle is not abstract and does not override abstract method area() in Shape:Circle is not abstract and does not override abstract method area() in Shape:
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java saysמדף, צעד 20: interface בשם LendableShelf, step 20: a Lendable interface
הצעד של היום בפרויקט מדףToday's step in the Shelf projectמה ההבדל העיקרי בין abstract class ל-interface?What's the main difference between an abstract class and an interface?
- מחלקה יכולה לממש כמה interfaces, אבל לרשת ממחלקה אחת בלבדA class can implement several interfaces, but extend only one class
- ב-interface אסור קוד בכללAn interface can't contain any code
- abstract class אי אפשר לרשתAn abstract class can't be extended
- אין הבדלThere's no difference
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.מחלקה יכולה לממש כמה interfaces, אבל לרשת ממחלקה אחת בלבדA class can implement several interfaces, but extend only one class
נכון. וגם: ל-abstract class יכולים להיות שדות ובנאי.Right. Also: an abstract class can have fields and a constructor.Circle extends Shape ולא מממשת את area() המופשטת. מה קורה?Circle extends Shape and doesn't implement the abstract area(). What happens?
- area מחזירה 0area returns 0
- javac עוצרjavac stops
- חריגה בזמן ריצהAn exception at run time
- זה בסדרThat's fine
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.javac עוצרjavac stops
נכון, אלא אם גם Circle מוגדרת abstract.Right, unless Circle is also declared abstract.a.compareTo(b) מחזיר -3. מה זה אומר?a.compareTo(b) returns -3. What does it mean?
- a בא לפני ba comes before b
- a בא אחרי ba comes after b
- יש שגיאהThere's an error
- a קטן מ-b בדיוק ב-3a is smaller than b by exactly 3
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.a בא לפני ba comes before b
נכון: שלילי = לפני, 0 = שווים, חיובי = אחרי.Right: negative = before, 0 = equal, positive = after.שכר לפי סוג עובדPay by employee type
abstract class Employee עם name ו-abstract int pay().abstract class Employee with a name and abstract int pay().
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מחיר עם מע"מPrice with VAT
interface Priced עם price(), label() ו-default double withVat() (פי 1.17).interface Priced with price(), label() and default double withVat() (times 1.17).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: שכחו לממשBug hunter: forgot to implement
- התוכנית לא מתקמפלת.The program doesn't compile.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.למיין תלמידיםSorting students
Student implements Comparable<Student> לפי הציון (מהנמוך לגבוה).Student implements Comparable<Student> by grade (low to high).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.static ו-default ב-interfacestatic and default in an interface
interface Shape עם area(), default boolean isBiggerThan(Shape other), ו-static double totalArea(Shape[] shapes).interface Shape with area(), default boolean isBiggerThan(Shape other), and static double totalArea(Shape[] shapes).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 20: interface בשם LendableShelf, step 20: a Lendable interface
interface Lendable עם name(), borrower(), lendTo, giveBack ו-default String status() (`Dune:interface Lendable with name(), borrower(), lendTo, giveBack and default String status() (`Dune:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.Template MethodTemplate Method
- ב-
abstract class Report יש final String render() שבונה כותרת, גוף וסיום, ושתי מתודות מופשטות:abstract class Report has final String render() building a header, a body and an ending, plus two abstract methods:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.שני default באותו שםTwo defaults with the same name
- ל-interfaces
A ול-B יש default String hello() (מחזירות A ו-B).Interfaces A and B both have default String hello() (returning A and B).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מה למדנוWhat we learned
- מחלקה מופשטת:
abstract class Shape:abstract class: abstract class Shape: - ל-
Shape אין דרך לחשב שטח, ולכן area מופשטת.Shape has no way to compute an area, so area is abstract. new Shape() לא מתקמפל: מה היה השטח שלו?new Shape() doesn't compile: what would its area be?Duck מממשת שני interfaces.Duck implements two interfaces.Circle is not abstract and does not override abstract method area() in Shape:Circle is not abstract and does not override abstract method area() in Shape:- מדף, צעד 20: interface בשם LendableShelf, step 20: a Lendable interface
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.