๐ฏ 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
defaultandstaticmethods 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
โ๏ธ 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.
| Feature | Interface | Abstract Class |
|---|---|---|
| Instance fields | โ | โ |
| Constructors | โ | โ |
| Abstract methods | โ | โ |
| Concrete methods | โ (default/static) | โ |
| Multiple | โ implement many | โ extend one |
| Use when | Shared contract | Shared 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
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.
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()).
๐ 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.
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
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.
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.
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.
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.
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
default method in an interface provide?Next: encapsulation and access modifiers โ
โ ๏ธ Common mistakes
static final.abstract or it won't compile. Every contract method must be fulfilled.default methods in interfaces are concrete but cannot access instance fields (interfaces have none). They can only call other interface methods.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.
defaultmethods add concrete behaviour to interfaces without breaking existing code.staticinterface methods belong to the interface type โ not inherited by implementors.- Multiple interfaces on one class is Java's substitute for multiple inheritance.
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 โ