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

חזרה למפגשBack
JavaJava

פולימורפיזם ו-dynamic dispatchPolymorphism & dynamic dispatch

מפגש 21: הפניה מטיפוס אחד ועצם מטיפוס אחר, איזו מתודה נבחרת בזמן ריצה, casting ו-instanceof.Session 21: A reference of one type and an object of another, which method is chosen at run time, casting and instanceof.

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

  • מונחיםTerms
  • משתנה אחד, הרבה צורותOne variable type, many forms
  • הטיפוס הסטטי קובע מה מותרThe static type decides what's allowed
  • instanceof עם משתנהinstanceof with a variable
  • overloading נבחר מוקדםOverloading is chosen early
  • טעויות נפוצותCommon mistakes
נושאTopic 1

מונחיםTerms

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

  • פולימורפיזם: "הרבה צורות":polymorphism: "Many forms":
  • טיפוס סטטי: הטיפוס של המשתנה, כפי שכתוב בקוד (Item b).static type: The variable's type as written in the code (Item b).
  • dynamic dispatch: בזמן ריצה, Java בוחרת את הגרסה של המתודה לפי הסוג האמיתי של העצם, לא לפי סוג המשתנה.dynamic dispatch: At run time, Java picks the method version by the object's real type, not the variable's type.
  • המרה למטה: (EBook) item:downcast: (EBook) item:
  • ClassCastException: החריגה כשממירים עצם לטיפוס שהוא לא:ClassCastException: The exception when you cast an object to a type it isn't:
נושאTopic 2

משתנה אחד, הרבה צורותOne variable type, many forms

משתנה אחד, הרבה צורותOne variable type, many forms

  • שלושת המשתנים הם מסוג Item, אבל כל אחד מצביע על עצם מסוג אחר.All three variables are of type Item, but each points to a different kind of object.
  • line() כתובה רק ב-Item, ובתוכה kind() ו-loanDays() נבחרות לפי העצם האמיתי (dynamic dispatch).line() is written only in Item, and inside it kind() and loanDays() are chosen by the real object (dynamic dispatch).
  • צפו בסימולציה: מ-line קופצים ל-kind של EBook.Watch the simulation: from line it jumps to EBook's kind.
נושאTopic 3

הטיפוס הסטטי קובע מה מותרThe static type decides what's allowed

הטיפוס הסטטי קובע מה מותרThe static type decides what's allowed

  • העצם הוא EBook, אבל המשתנה הוא Item, ול-Item אין getSizeMb.The object is an EBook, but the variable is an Item, and Item has no getSizeMb.
  • javac בודק לפי הטיפוס הסטטי ועוצר.javac checks by the static type and stops.
נושאTopic 4

instanceof עם משתנהinstanceof with a variable

instanceof עם משתנהinstanceof with a variable

  • it instanceof EBook e בודק את הסוג ונותן משתנה e מסוג EBook, כך שמותר לקרוא ל-getSizeMb.it instanceof EBook e checks the type and gives a variable e of type EBook, so you may call getSizeMb.
  • בלי הבדיקה, המרה של Dvd הייתה זורקת ClassCastException.Without the check, a downcast of a Dvd would throw a ClassCastException.
נושאTopic 5

overloading נבחר מוקדםOverloading is chosen early

overloading נבחר מוקדםOverloading is chosen early

  • שני המשתנים מצביעים על אותה מחרוזת, אבל what(o) בוחרת את what(Object): גרסה של overloading נבחרת בזמן קומפילציה, לפי הטיפוס הסטטי.Both variables point to the same string, but what(o) picks what(Object): an overload is chosen at compile time, by the static type.
  • רק overriding נבחר בזמן ריצה.Only overriding is chosen at run time.
נושאTopic 6

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

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

  • ClassCastException בשורה 6:ClassCastException on line 6:
נושאTopic 7

פרויקט מדףShelf project

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

  • מדף, צעד 21: רשימה אחת לכל הסוגיםShelf, step 21: one list for every kind

פולימורפיזם: "הרבה צורות":polymorphism: "Many forms":

טיפוס סטטי: הטיפוס של המשתנה, כפי שכתוב בקוד (Item b).static type: The variable's type as written in the code (Item b).

dynamic dispatch: בזמן ריצה, Java בוחרת את הגרסה של המתודה לפי הסוג האמיתי של העצם, לא לפי סוג המשתנה.dynamic dispatch: At run time, Java picks the method version by the object's real type, not the variable's type.

המרה למטה: (EBook) item:downcast: (EBook) item:

ClassCastException: החריגה כשממירים עצם לטיפוס שהוא לא:ClassCastException: The exception when you cast an object to a type it isn't:

שלושת המשתנים הם מסוג Item, אבל כל אחד מצביע על עצם מסוג אחר.All three variables are of type Item, but each points to a different kind of object.

line() כתובה רק ב-Item, ובתוכה kind() ו-loanDays() נבחרות לפי העצם האמיתי (dynamic dispatch).line() is written only in Item, and inside it kind() and loanDays() are chosen by the real object (dynamic dispatch).

צפו בסימולציה: מ-line קופצים ל-kind של EBook.Watch the simulation: from line it jumps to EBook's kind.

העצם הוא EBook, אבל המשתנה הוא Item, ול-Item אין getSizeMb.The object is an EBook, but the variable is an Item, and Item has no getSizeMb.

javac בודק לפי הטיפוס הסטטי ועוצר.javac checks by the static type and stops.

it instanceof EBook e בודק את הסוג ונותן משתנה e מסוג EBook, כך שמותר לקרוא ל-getSizeMb.it instanceof EBook e checks the type and gives a variable e of type EBook, so you may call getSizeMb.

בלי הבדיקה, המרה של Dvd הייתה זורקת ClassCastException.Without the check, a downcast of a Dvd would throw a ClassCastException.

שני המשתנים מצביעים על אותה מחרוזת, אבל what(o) בוחרת את what(Object): גרסה של overloading נבחרת בזמן קומפילציה, לפי הטיפוס הסטטי.Both variables point to the same string, but what(o) picks what(Object): an overload is chosen at compile time, by the static type.

רק overriding נבחר בזמן ריצה.Only overriding is chosen at run time.

ClassCastException בשורה 6:ClassCastException on line 6:

מדף, צעד 21: רשימה אחת לכל הסוגיםShelf, step 21: one list for every kind

Item x = new Dvd("Alien"); x.loanDays() — של מי ה-loanDays שרצה?Item x = new Dvd("Alien"); x.loanDays() — whose loanDays runs?

  • של ItemItem's
  • של DvdDvd's
  • שגיאת קומפילציהA compile error
  • תלוי בסדר המחלקות בקובץIt depends on the class order in the file

של DvdDvd's

Item x = new Dvd("Alien"); EBook e = (EBook) x; מה קורה?Item x = new Dvd("Alien"); EBook e = (EBook) x; What happens?

  • מתקמפל, וזורק ClassCastException בריצהIt compiles, and throws a ClassCastException at run time
  • שגיאת קומפילציהA compile error
  • e יהיה nulle will be null
  • זה עובדIt works

מתקמפל, וזורק ClassCastException בריצהIt compiles, and throws a ClassCastException at run time

Object o = "hi"; ויש f(Object) ו-f(String). מה נקרא ב-f(o)?Object o = "hi"; with f(Object) and f(String). Which runs for f(o)?

  • f(String)f(String)
  • f(Object)f(Object)
  • שתיהןBoth
  • שגיאהAn error

f(Object)f(Object)

סך כל השטחיםTotal area

  • במערך Shape[] יש שני עיגולים (רדיוס 1 ו-2) ומלבן 2x3.A Shape[] array holds two circles (radius 1 and 2) and a 2x3 rectangle.

גן חיותA zoo

  • מערך של Animal עם כלב, חתול וכלב.An Animal array with a dog, a cat and a dog.

צייד באגים: המרה עיוורתBug hunter: a blind cast

  • התוכנית סוכמת את הגודל של הספרים הדיגיטליים, וקורסת על ה-DVD.The program sums the sizes of the ebooks, and crashes on the DVD.

overloading ו-overriding יחדOverloading and overriding together

  • ל-Greeter יש שתי גרסאות של greet (עם String ועם int).Greeter has two versions of greet (with a String and with an int).

מתודה שמקבלת את ההורהA method that takes the parent

  • כתבו static int maxLoan(Item[] items) ו-static int minLoan(Item[] items).Write static int maxLoan(Item[] items) and static int minLoan(Item[] items).

מדף, צעד 21: רשימה אחת לכל הסוגיםShelf, step 21: one list for every kind

  • ב-List<Item> יש Emma (ספר), Dune (ebook), Alien (dvd) ו-Emma (ebook).A List<Item> holds Emma (a book), Dune (ebook), Alien (dvd) and Emma (ebook).

מה למדנוWhat we learned

  • פולימורפיזם: "הרבה צורות":polymorphism: "Many forms":
  • שלושת המשתנים הם מסוג Item, אבל כל אחד מצביע על עצם מסוג אחר.All three variables are of type Item, but each points to a different kind of object.
  • העצם הוא EBook, אבל המשתנה הוא Item, ול-Item אין getSizeMb.The object is an EBook, but the variable is an Item, and Item has no getSizeMb.
  • it instanceof EBook e בודק את הסוג ונותן משתנה e מסוג EBook, כך שמותר לקרוא ל-getSizeMb.it instanceof EBook e checks the type and gives a variable e of type EBook, so you may call getSizeMb.
  • שני המשתנים מצביעים על אותה מחרוזת, אבל what(o) בוחרת את what(Object): גרסה של overloading נבחרת בזמן קומפילציה, לפי הטיפוס הסטטי.Both variables point to the same string, but what(o) picks what(Object): an overload is chosen at compile time, by the static type.
  • ClassCastException בשורה 6:ClassCastException on line 6:

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

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

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