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

חזרה למפגשBack
JavaJava

abstract ו-interfacesabstract & interfaces

מפגש 20: מחלקות מופשטות, interfaces עם default ו-static methods, ומימוש של כמה interfaces.Session 20: Abstract classes, interfaces with default and static methods, and implementing several interfaces.

מה נעשה היום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
נושאTopic 1

מונחיםTerms

המונחים של המפגש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.
נושא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).
נושא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?
נושא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.
נושא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:
נושאTopic 6

פרויקט מדףShelf project

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

  • מדף, צעד 20: interface בשם LendableShelf, step 20: a Lendable interface

מחלקה מופשטת: 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.

ל-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).

new Shape() לא מתקמפל: מה היה השטח שלו?new Shape() doesn't compile: what would its area be?

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.

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

מה ההבדל העיקרי בין 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

מחלקה יכולה לממש כמה interfaces, אבל לרשת ממחלקה אחת בלבדA class can implement several interfaces, but extend only one class

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

javac עוצרjavac stops

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

a בא לפני ba comes before b

שכר לפי סוג עובדPay by employee type

  • abstract class Employee עם name ו-abstract int pay().abstract class Employee with a name and abstract int pay().

מחיר עם מע"מPrice with VAT

  • interface Priced עם price(), label() ו-default double withVat() (פי 1.17).interface Priced with price(), label() and default double withVat() (times 1.17).

צייד באגים: שכחו לממשBug hunter: forgot to implement

  • התוכנית לא מתקמפלת.The program doesn't compile.

למיין תלמידיםSorting students

  • Student implements Comparable<Student> לפי הציון (מהנמוך לגבוה).Student implements Comparable<Student> by grade (low to high).

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).

מדף, צעד 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:

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:

שני 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).

מה למדנו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

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

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

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