انتقل إلى المحتوى
أمين أبوزيد
BEG-024

الفئات المجرّدة والواجهات

  • 6 درس
  • 64 دقيقة قراءة
  • 3 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • الدوال المجرّدة
  • عقود الواجهات
  • الدوال الافتراضية (default methods)

تعلّم أولًا

64 دقيقة قراءة · 3 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Declare an interface as a contract and implement it in several classes.
  2. Write an abstract class with shared state and abstract methods.
  3. Choose between an interface and an abstract class for a given scenario.
  4. Use default methods and explain their purpose and risks.

الدروس

  1. 01الواجهات كعقود11 د قراءة
  2. 02الفئات المجرّدة (Abstract classes)11 د قراءة
  3. 03الواجهة مقابل الفئة المجرّدة: دليل القرار11 د قراءة
  4. 04الدوال الافتراضية والثابتة في الواجهات11 د قراءة
  5. 05البرمجة نحو واجهة9 د قراءة
  6. 06عرض عملي: قنوات إشعارات قابلة للتبديل11 د قراءة

المختبر والتمارين

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)

IDOutcomeTask
BEG-024-EX011Declare Shape with double area() and implement it in Circle and Rectangle.
BEG-024-EX022Write an abstract Employee with name, a constructor, and abstract double pay(); implement Salaried and Hourly.
BEG-024-EX034Add a default String describe() to Shape that returns "Area: " + area(). Name one risk of default methods.
BEG-024-EX043Interface or abstract class? (a) Flyable for birds and planes, (b) a base Vehicle with shared speed and fuel fields.
BEG-024-EX053Design 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(); }; Circle returns Math.PI * r * r; Rectangle returns w * h.
  • EX02 abstract class Employee { protected final String name; Employee(String n) { name = n; } abstract double pay(); } with two subclasses overriding pay.
  • 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); } with PdfExporter, 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.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1A class uses an interface with the keyword:O1
  2. 2A class can implement:O1
  3. 3Interface methods are by default:O1
  4. 4You can create an object of an abstract class with new:O2
  5. 5An abstract class can contain:O2
  6. 6Two unrelated classes need a shared capability. Use:O3
  7. 7Closely related classes share fields and code. Prefer:O3
  8. 8A default method:O4
  9. 9A main reason for default methods is to:O4
  10. 10"Program to an interface" means:O3

الجزء العملي

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.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.