الفئات المجرّدة والواجهات
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- الدوال المجرّدة
- عقود الواجهات
- الدوال الافتراضية (default methods)
تعلّم أولًا
64 دقيقة قراءة · 3 ساعة تدريب (تقدير)
مخرجات التعلم
- Declare an interface as a contract and implement it in several classes.
- Write an abstract class with shared state and abstract methods.
- Choose between an interface and an abstract class for a given scenario.
- Use default methods and explain their purpose and risks.
الدروس
المختبر والتمارين
Lab
BEG-024-LAB1 · Notification channels
Outcomes: 1, 3 · Time: ~2 h Create a Notifier interface with Email, SMS, and Console implementations. The bank sends statements through any Notifier. Add one test or main that runs the same statement through all three. Acceptance: the bank class depends only on the interface.
Exercises (assessment: 4 exercises plus a design question)
| ID | Outcome | Task |
|---|---|---|
| BEG-024-EX01 | 1 | Declare Shape with double area() and implement it in Circle and Rectangle. |
| BEG-024-EX02 | 2 | Write an abstract Employee with name, a constructor, and abstract double pay(); implement Salaried and Hourly. |
| BEG-024-EX03 | 4 | Add a default String describe() to Shape that returns "Area: " + area(). Name one risk of default methods. |
| BEG-024-EX04 | 3 | Interface or abstract class? (a) Flyable for birds and planes, (b) a base Vehicle with shared speed and fuel fields. |
| BEG-024-EX05 | 3 | Design question: a report system must export to PDF, CSV, and later more formats. Sketch the types and justify interface vs abstract class. |
أظهر الحلول
- EX01
interface Shape { double area(); };CirclereturnsMath.PI * r * r;Rectanglereturnsw * h. - EX02
abstract class Employee { protected final String name; Employee(String n) { name = n; } abstract double pay(); }with two subclasses overridingpay. - EX03
default String describe() { return "Area: " + area(); }. Risk: conflicts when two interfaces define the same default, and logic hidden in interfaces. - EX04 (a) interface (unrelated types share a capability); (b) abstract class (shared state).
- EX05
interface ReportExporter { void export(Report r); }withPdfExporter,CsvExporter; add an abstract helper only if they share real code. New formats are new classes. - Outcome 1 is also covered by the lab.
الاختبار
Outcomes: O1 interfaces · O2 abstract classes · O3 choosing · O4 default methods.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
Four exercises plus the design question (BEG-024-EX01 to EX05) and the notification lab. Pass: at least four of five correct and the bank class depends only on Notifier.
أخطاء شائعة
Interfaces with a single "god" method; abstract classes used where an interface suffices.
التقييم
4 exercises plus a design question.
الصلة بالمسار الوظيفي
Every Spring repository and service boundary (JNR-009, JNR-029) is an interface.