๐ŸŽฏ What you will learn

Java gives you two tools for defining shared structure: interfaces and abstract classes. They look similar but serve different purposes. By the end of this lesson you will understand:

  • What an interface is โ€” and how default and static methods extend it
  • What an abstract class is โ€” and when shared state requires one
  • How to implement multiple interfaces on a single class
  • The key differences between the two โ€” and when to choose each
  • How NaijaCart uses all three patterns in production
๐Ÿ’ผ Interview reality: "Interface vs abstract class" is one of the most common Java OOP interview questions. The answer hinges on two facts: interfaces have no instance fields, and a class can implement many interfaces but extend only one class.

โš–๏ธ Interface vs abstract class

An interface is a pure contract โ€” it defines what a class can do. Since Java 8 it can also include default (concrete, inherited) and static (type-level, not inherited) methods, but it cannot hold mutable instance state.

An abstract class is a partial implementation โ€” it defines some shared state (instance fields) and behaviour while leaving gaps (abstract methods) for subclasses to fill. A class can extend only one abstract class.

FeatureInterfaceAbstract Class
Instance fieldsโŒโœ…
ConstructorsโŒโœ…
Abstract methodsโœ…โœ…
Concrete methodsโœ… (default/static)โœ…
Multipleโœ… implement manyโŒ extend one
Use whenShared contractShared state + structure

Rule of thumb: reach for an interface when you want to describe a capability (Taxable, Auditable). Reach for an abstract class when you want to share state and partial behaviour across a family of related types (FinancialAccount).

๐Ÿ›’ Real-world scenario

๐Ÿ›’
Real-world scenario ยท NaijaCart (Lagos)
NaijaCart โ€” Tunde's payment and account contracts

NaijaCart is a Lagos-based e-commerce platform processing 50,000 daily transactions. Tunde, a backend engineer, needs to model: (1) a tax computation contract that any income type must implement, (2) a bank account hierarchy where each account type has mandatory fee logic, and (3) a transaction that simultaneously satisfies Auditable, Notifiable, and Reversible contracts โ€” impossible with a single class hierarchy, easy with multiple interfaces.

๐Ÿงฐ Interfaces โ€” contract + default + static

An interface method with no modifier is public abstract by default. A default method provides an implementation that subclasses inherit and can optionally override. A static method belongs to the interface type itself โ€” not inherited.

javac InterfaceBasics.java && java InterfaceBasics
InterfaceBasics.javaNaijaCart ยท Tax computation engine
verified output
=== NaijaCart โ€” Tax Computation Engine === Freelancer income: โ‚น850,000.00 tax(HIGH 30%): โ‚น255,000.00 Salaried income: โ‚น600,000.00 tax(MEDIUM 10%): โ‚น60,000.00 Salaried income: โ‚น1,500,000.00 tax(MEDIUM 20%): โ‚น300,000.00

Key point: taxAmount() is a default method โ€” it calls the abstract taxRate() which each implementing class provides. The static category() is called via the interface name: Taxable.category(rate) โ€” never via an instance.

๐Ÿ—๏ธ Abstract classes โ€” shared state and structure

Use an abstract class when related types share fields and some behaviour. The abstract class owns the shared state (id, balance); subclasses fill in the gaps (monthlyFee(), accountType()).

javac AbstractBasics.java && java AbstractBasics
AbstractBasics.javaNaijaCart ยท Monthly fee cycle
verified output
=== NaijaCart โ€” Monthly Fee Cycle === Zero-Balance SB-ZB-001 fee: โ‚น0.00 balance after: โ‚น0.00 Premium SB-PM-002 fee: โ‚น299.00 balance after: โ‚น49,701.00 Business SB-BZ-003 fee: โ‚น999.00 balance after: โ‚น499,001.00

๐Ÿ”— Multiple interfaces on one class

A single class can implement as many interfaces as needed โ€” this is Java's substitute for multiple inheritance. CartTransaction below simultaneously satisfies Auditable, Notifiable, and Reversible, letting any service that expects any of those types work with it polymorphically.

javac MultiInterface.java && java MultiInterface
MultiInterface.javaNaijaCart ยท Transaction lifecycle
verified output
=== NaijaCart โ€” Transaction Lifecycle === [AUDIT] TXN-NG-9901 customer=TUNDE-042 โ‚ฆ45,000.00 status=COMPLETED [SMS] Notified TUNDE-042 โ€” txn TXN-NG-9901 โ‚ฆ45,000.00 [EMAIL] Notified TUNDE-042 โ€” txn TXN-NG-9901 โ‚ฆ45,000.00 Reversed TXN-NG-9901 โ‚ฆ45,000.00 โ†’ customer TUNDE-042 [AUDIT] TXN-NG-9901 customer=TUNDE-042 โ‚ฆ45,000.00 status=REVERSED Second reverse attempt: false

Design insight: Any service that only needs to audit can accept an Auditable reference โ€” it doesn't need to know the concrete type. This is the Dependency Inversion Principle in practice.

๐ŸŽค Interview Q&A

EasyWhen would you use an interface vs an abstract class?
+

Use an interface when you want to define a contract (what a class can do) with no shared state โ€” multiple unrelated classes can implement it. Use an abstract class when you have shared state (instance fields) or a partial implementation that related classes should inherit. Rule of thumb: interfaces for capabilities, abstract classes for family.

EasyWhat is a default method in a Java interface?
+

Introduced in Java 8, a default method provides a concrete implementation inside an interface. It lets you add new methods to existing interfaces without breaking all implementing classes โ€” they inherit the default behaviour unless they override it. The classic use case is adding utility methods (like taxAmount()) derived from the abstract method.

MediumA class implements two interfaces with the same default method name. What happens?
+

The compiler reports an ambiguity error โ€” you must explicitly override the method in your class to resolve which version to use (or write your own). You can still delegate to a specific interface's version using InterfaceName.super.method(). This is the only "conflict" scenario Java requires you to resolve manually.

MediumWhy can't interfaces have instance fields?
+

Interfaces define contracts between unrelated classes. If interfaces had mutable instance state, they would impose a memory layout on every implementing class, which would conflict with multiple implementation. Interfaces can only have static final constants (implicitly public) โ€” effectively named constants, not mutable state.

HardWhat is the difference between an interface's static method and a default method in terms of inheritance?
+

A default method is inherited by implementing classes and can be overridden. A static method on an interface belongs to the interface type itself โ€” it is NOT inherited by implementing classes. You must call it via the interface name (Taxable.category(rate)), not via an instance or subclass. This mirrors the behaviour of static methods on regular classes.

๐Ÿ“ Quiz โ€” 5 questions

Question 1 of 5
A class can implement:
Question 2 of 5
What does a default method in an interface provide?
Question 3 of 5
Can you instantiate an abstract class directly?
Question 4 of 5
An interface method with no modifier is:
Question 5 of 5
Which keyword is used to implement an interface?
0
/ 5
Quiz complete!

Next: encapsulation and access modifiers โ†’

โš ๏ธ Common mistakes

Adding instance fields to an interface
Interfaces cannot hold mutable instance state. Use an abstract class if you need shared fields. Interface constants must be static final.
Forgetting to implement all interface methods
If any abstract method is not implemented, the class must be declared abstract or it won't compile. Every contract method must be fulfilled.
Confusing default method with instance method
default methods in interfaces are concrete but cannot access instance fields (interfaces have none). They can only call other interface methods.
Not overriding toString in abstract class
Abstract classes inherit from Object. Forgetting to override toString() means objects print as hex addresses when logged โ€” always override for debuggability.

โœ… Key takeaways

  • Interfaces define contracts; abstract classes define partial implementations with state.
  • A class can implement many interfaces but extend only one class.
  • default methods add concrete behaviour to interfaces without breaking existing code.
  • static interface methods belong to the interface type โ€” not inherited by implementors.
  • Multiple interfaces on one class is Java's substitute for multiple inheritance.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

From contracts to production Spring Boot

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’