Java OOP · Module 11
Multiple interfaces, default & static methods
Many capabilities on one class — and the modern interface toolkit
What you'll be able to do
- Implement several interfaces on one class and use each view
- Explain why multiple interfaces are safe while multiple superclasses are not
- Use `default`, `static` and `private` methods inside an interface
- Choose between an interface and an abstract class with a clear rule
Multiple Interfaces
A class can sign many contracts
A duck flies and swims. A plane only flies. A fish only swims. One inheritance tree cannot express that — two interfaces can.
class Duck implements Flyable, Swimmable — separate the interfaces with commas and implement every method of every one.
Why is this allowed when extends A, B is not? Because of state. If two superclasses both had a field speed and a method move(), the object would carry two copies of the data and Java could not tell which move() you meant — the classic *diamond problem*. Interfaces (before Java 8) carry no data and no bodies, so there is nothing to duplicate: only the *requirement* is inherited, and one implementation satisfies both.
Add `interface Quackable { void quack(); }` to Duck as a third contract, then call it through a `Quackable` reference.
Same signature in two interfaces? No problem
If Printable and Exportable both declare void render();, a class implementing both writes one render() method and it satisfies both contracts. There is no conflict, because neither interface supplied a body.
Prove it — coding challenges
Default Methods
default methods: bodies inside an interface
Java 8 added default methods so that an interface could gain a new method without breaking the thousands of classes that already implement it. A default method has a body; implementing classes inherit it and may override it.
That backwards-compatibility problem is the real reason they exist — not as a replacement for abstract classes. An interface still has no instance fields, so a default method can only work with the other methods of the contract.
Static Interface Methods
static and private methods in interfaces
Four kinds of interface member
InterfaceName.method(), not inherited.Sandbox note
The in-browser Java engine used by the challenges focuses on the classic OOP core, so default, static and private interface methods (and lambdas) are shown here as reference code rather than runnable examples. Everything the challenges ask you to write — interfaces, implements, overriding, polymorphism — runs for real.
Interface vs Abstract Class
Interface vs abstract class — the decision
Full comparison
| Aspect | Interface | Abstract class |
|---|---|---|
| Keyword to attach | implements | extends |
| How many per class | unlimited | exactly one |
| Instance fields | none (constants only) | yes |
| Constructors | none | yes |
| Method bodies | default / static / private only | any concrete method |
| Member access levels | public (private only for helpers) | public, protected, package-private, private |
| Relationship expressed | CAN-DO (capability) | IS-A (family) |
| Best for | unrelated types sharing a capability; swappable implementations | related types sharing state and code |
| Instantiable? | no | no |
| Polymorphism | yes | yes |
Rule of thumb: need shared **state** or a protected algorithm → abstract class. Need a capability that unrelated classes can also have, or several capabilities on one class → interface. Combine both when the design calls for it.
Three questions that settle it
- Do the subtypes need to share fields? Yes → abstract class.
- Might a class need this *and* other unrelated capabilities? Yes → interface.
- Is this a real family (
SavingsAccount IS-A Account) or just a capability (Account CAN-DO transfer)? Family → abstract class; capability → interface.
Common mistakes
Trying to put instance fields in an interface: `private int speed;`Assuming a class inherits `static` interface methods.Ignoring a default-method conflict and hoping the compiler picks one.Lesson playground
CardPayment signs two contracts; CashPayment signs one. Add a `WalletPayment` that is Payable and Refundable, and refund it too.
Check understanding
Quiz
Question 1 of 5
One class, two interfaces, two views. What is printed?
Summary
- A class may implement any number of interfaces — safe because interfaces carry no instance state.
- `default` methods let an interface evolve without breaking existing implementations.
- `static` interface methods are utilities; `private` ones share code between defaults.
- Shared state → abstract class; shared capability across unrelated types → interface; often both together.