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

JavaJava
20
חלקPart 4: OOP מתקדםAdvanced OOP כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

abstract ו-interfacesabstract & interfaces

מחלקות מופשטות, interfaces עם default ו-static methods, ומימוש של כמה interfaces.

Abstract classes, interfaces with default and static methods, and implementing several interfaces.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • מחלקה מופשטת: abstract class Shape:abstract class: abstract class Shape:
  • מתודה מופשטת: abstract double area();:abstract method: abstract double area();:
  • ל-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.
  • 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

מונחיםTerms

  • מחלקה מופשטתabstract class: abstract class Shape: אי אפשר ליצור ממנה עצם (new Shape() לא מתקמפל), רק לרשת ממנה. יכולה להחזיק שדות, בנאים וקוד משותף.abstract class Shape: you can't make an object of it (new Shape() doesn't compile), only extend it. It can hold fields, constructors and shared code.
  • מתודה מופשטתabstract method: abstract double area();: כותרת בלי גוף. כל תת-מחלקה לא מופשטת חייבת לממש אותה, אחרת javac עוצר.abstract double area();: a header with no body. Every non-abstract subclass must implement it, or javac stops.
  • interfaceinterface: חוזה: רשימת מתודות שמחלקה מבטיחה לממש (implements). מחלקה יכולה לממש כמה interfaces. אין בו שדות של עצם.A contract: a list of methods a class promises to implement (implements). A class can implement several interfaces. It has no object fields.
  • מתודת defaultdefault method: מתודה עם גוף בתוך interface. כל מי שמממש מקבל אותה, ויכול לדרוס. היא בנויה מהמתודות האחרות של ה-interface.A method with a body inside an interface. Every implementer gets it, and can override it. It's built from the interface's other methods.
  • ComparableComparable: Comparable<T> מגדיר "סדר טבעי": compareTo מחזיר מספר שלילי, 0 או חיובי. Arrays.sort ו-Collections.sort משתמשים בו.Comparable<T> defines a "natural order": compareTo returns a negative number, 0 or a positive one. Arrays.sort and Collections.sort use it.
  • Template MethodTemplate Method: מתודה בהורה (לרוב final) שקובעת את סדר השלבים, ומשאירה שלבים מופשטים לתת-המחלקות.A method in the parent (often final) that fixes the order of the steps, leaving abstract steps to the subclasses.
  • מחלקה מופשטתabstract class: abstract class Shape: אי אפשר ליצור ממנה עצם (new Shape() לא מתקמפל), רק לרשת ממנה. יכולה להחזיק שדות, בנאים וקוד משותף.abstract class Shape: you can't make an object of it (new Shape() doesn't compile), only extend it. It can hold fields, constructors and shared code.
  • מתודה מופשטתabstract method: abstract double area();: כותרת בלי גוף. כל תת-מחלקה לא מופשטת חייבת לממש אותה, אחרת javac עוצר.abstract double area();: a header with no body. Every non-abstract subclass must implement it, or javac stops.
  • interfaceinterface: חוזה: רשימת מתודות שמחלקה מבטיחה לממש (implements). מחלקה יכולה לממש כמה interfaces. אין בו שדות של עצם.A contract: a list of methods a class promises to implement (implements). A class can implement several interfaces. It has no object fields.
  • מתודת defaultdefault method: מתודה עם גוף בתוך interface. כל מי שמממש מקבל אותה, ויכול לדרוס. היא בנויה מהמתודות האחרות של ה-interface.A method with a body inside an interface. Every implementer gets it, and can override it. It's built from the interface's other methods.
  • ComparableComparable: Comparable<T> מגדיר "סדר טבעי": compareTo מחזיר מספר שלילי, 0 או חיובי. Arrays.sort ו-Collections.sort משתמשים בו.Comparable<T> defines a "natural order": compareTo returns a negative number, 0 or a positive one. Arrays.sort and Collections.sort use it.
  • Template MethodTemplate Method: מתודה בהורה (לרוב final) שקובעת את סדר השלבים, ומשאירה שלבים מופשטים לתת-המחלקות.A method in the parent (often final) that fixes the order of the steps, leaving abstract steps to the subclasses.

Cheatsheet

מחלקה מופשטת: תבנית חלקיתAn abstract class: a partial blueprint
Shape c = new Circle(1);
Shape s = new Square(2);
System.out.println(c.describe());
System.out.println(s.describe());
interface, default, וכמה interfacesinterface, default, and several interfaces
Duck d = new Duck();
System.out.println(d.fly() + ", " + d.swim());
System.out.println(d.describe());
Flyer f = d;
System.out.println(f.fly());

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do
Shape is abstract; cannot be instantiatedאי אפשר ליצור עצם מופשטYou can't create an abstract objectShape is abstract; cannot be instantiated: יוצרים Circle או Square, לא Shape.Shape is abstract; cannot be instantiated: create a Circle or a Square, not a Shape.
Circle is not abstract and does not override abstract methodצייד באגים: שכחו לממשBug hunter: forgot to implementCircle is not abstract and does not override abstract method area() in Shape: חסר מימוש של area.Circle is not abstract and does not override abstract method area() in Shape: area has no implementation.

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