Skip to content
Ameen Abozaid
BEG-024

Abstract classes and interfaces

  • 6 Lessons
  • 64 min of reading
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Abstract methods
  • interface contracts
  • default methods

Learn first

64 min of reading · 3 h of practice (estimate)

Learning outcomes

  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.

Lessons

  1. 01Interfaces as contracts11 min read
  2. 02Abstract classes11 min read
  3. 03Interface vs abstract class: decision guide11 min read
  4. 04Default and static interface methods11 min read
  5. 05Programming to an interface9 min read
  6. 06Demo: pluggable notification channels11 min read

Lab and exercises

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.
Show solutions
  • 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.

Quiz

Outcomes: O1 interfaces · O2 abstract classes · O3 choosing · O4 default methods.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  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

Practical part

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.

Common mistakes

Interfaces with a single "god" method; abstract classes used where an interface suffices.

Assessment

4 exercises plus a design question.

Career relevance

Every Spring repository and service boundary (JNR-009, JNR-029) is an interface.

Type to search levels, phases, modules and lessons.