חלק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.
הנקודות החשובותKey points
- מחלקה מופשטת:
abstract class Shape:abstract class:abstract class Shape: - מתודה מופשטת:
abstract double area();:abstract method:abstract double area();: - ל-
Shapeאין דרך לחשב שטח, ולכןareaמופשטת.Shapehas no way to compute an area, soareais abstract. describeכתובה פעם אחת בהורה, ובזמן ריצה קוראת ל-areaשל העצם האמיתי (עיגול או ריבוע).describeis written once in the parent, and at run time it calls the real object'sarea(circle or square).new Shape()לא מתקמפל: מה היה השטח שלו?new Shape()doesn't compile: what would its area be?Duckמממשת שני interfaces.Duckimplements two interfaces.- מ-
Flyerהיא מקבלת אתdescribe(מתודת default) בחינם.FromFlyerit getsdescribe(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":compareToreturns a negative number, 0 or a positive one.Arrays.sortandCollections.sortuse it. - Template MethodTemplate Method: מתודה בהורה (לרוב
final) שקובעת את סדר השלבים, ומשאירה שלבים מופשטים לתת-המחלקות.A method in the parent (oftenfinal) 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":compareToreturns a negative number, 0 or a positive one.Arrays.sortandCollections.sortuse it. - Template MethodTemplate Method: מתודה בהורה (לרוב
final) שקובעת את סדר השלבים, ומשאירה שלבים מופשטים לתת-המחלקות.A method in the parent (oftenfinal) 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 object | Shape 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 implement | Circle 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. |