One parent, several capabilities
Use an interface to define what a type can do, especially when unrelated classes share a capability. Use an abstract class when closely related classes share state and code. Often the best design is both: an interface for the contract and an abstract class that implements most of it.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Design Notifier as an interface and AbstractNotifier as a skeletal class that stores a sentCount and implements send(String) by calling an abstract deliver(String) and incrementing the count. Implement EmailNotifier.
Using AbstractList, write Repeat(String s, int n) that looks like a list of n copies of s. Print it and its size().
Decide interface or abstract class for each and justify in one line: (a) Closeable, (b) a base HttpServlet with service() dispatching to doGet/doPost, (c) PaymentGateway with Stripe and Razorpay implementations.
Explain it without notes
Give four concrete differences between an abstract class and an interface in modern Java.
When would you choose an abstract class over an interface?
What is a skeletal implementation? Give a JDK example.
Why is 'start with an interface' usually the safer default?
Expected output
Printing Invoice #1
Invoice #2 as PDF
Amount: 500
Invoice #1 / Invoice #1 as CSV