Lab 13: Interfaces Advanced

Objective

Master all four interface member types in Java 9+: abstract, default, static, and private methods. Build composable interfaces for pricing, validation, and reporting using the template method pattern via defaults and interface inheritance chains.

Background

Since Java 8, interfaces can have default methods (inherited implementations) and static methods (utility factories). Java 9 added private methods in interfaces — shared implementation for default methods without exposing it publicly. This makes interfaces much more powerful than simple contracts: they can carry behaviour while still permitting multiple inheritance without the diamond problem.

Time

25 minutes

Prerequisites

  • Lab 12 (Reflection & Annotations)

Tools

  • Docker: zchencow/innozverse-java:latest


Lab Instructions

Steps 1–8: Default/static/private interface methods, composition, PECS with interfaces, template method, marker interfaces, Capstone

💡 private methods in interfaces (Java 9+) solve the "shared default method code" problem. Before Java 9, if two default methods needed shared logic, you had to either duplicate it or expose a default helper method that wasn't part of the public API. private interface methods hide that shared implementation cleanly — exactly like private helper methods in classes.

📸 Verified Output:


Summary

Interface member
Since
Inherited?
Overridable?

abstract method

Java 1

No (must implement)

N/A

default method

Java 8

Yes

Yes

static method

Java 8

No

No

private method

Java 9

No

No

Further Reading

Last updated