Command Palette

Search for a command to run...

Back to the lesson: Topic 5.9 — Abstract Class vs Interface
Core Java · Example 3 of 3

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.

01

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.

02

Using AbstractList, write Repeat(String s, int n) that looks like a list of n copies of s. Print it and its size().

03

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

01

Give four concrete differences between an abstract class and an interface in modern Java.

02

When would you choose an abstract class over an interface?

03

What is a skeletal implementation? Give a JDK example.

04

Why is 'start with an interface' usually the safer default?

One parent, several capabilities
Sign in to run this example in your browser.

Expected output

Printing Invoice #1
Invoice #2 as PDF
Amount: 500
Invoice #1 / Invoice #1 as CSV