-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdefault-interface-methods.html
More file actions
351 lines (332 loc) Β· 14.5 KB
/
default-interface-methods.html
File metadata and controls
351 lines (332 loc) Β· 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Default interface methods | java.evolved</title>
<meta name="description" content="Add method implementations directly in interfaces, enabling multiple inheritance of behavior.">
<meta name="robots" content="index, follow">
<link rel="canonical" href="https://javaevolved.github.io/default-interface-methods.html">
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.svg" type="image/svg+xml">
<link rel="manifest" href="manifest.json">
<meta name="theme-color" content="#f97316">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-mobile-web-app-title" content="java.evolved">
<meta property="og:title" content="Default interface methods | java.evolved">
<meta property="og:description" content="Add method implementations directly in interfaces, enabling multiple inheritance of behavior.">
<meta property="og:url" content="https://javaevolved.github.io/default-interface-methods.html">
<meta property="og:type" content="article">
<meta property="og:site_name" content="java.evolved">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://javaevolved.github.io/images/social-card.png">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
<meta property="og:image:type" content="image/png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Default interface methods | java.evolved">
<meta name="twitter:description" content="Add method implementations directly in interfaces, enabling multiple inheritance of behavior.">
<meta name="twitter:image" content="https://javaevolved.github.io/images/social-card.png">
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "TechArticle",
"headline": "Default interface methods",
"description": "Add method implementations directly in interfaces, enabling multiple inheritance of behavior.",
"url": "https://javaevolved.github.io/default-interface-methods.html",
"publisher": {
"@type": "Organization",
"name": "java.evolved",
"url": "https://javaevolved.github.io"
},
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://javaevolved.github.io/default-interface-methods.html"
}
}
</script>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{
"@type": "ListItem",
"position": 1,
"name": "Home",
"item": "https://javaevolved.github.io/"
},
{
"@type": "ListItem",
"position": 2,
"name": "Language",
"item": "https://javaevolved.github.io/?cat=language"
},
{
"@type": "ListItem",
"position": 3,
"name": "Default interface methods"
}
]
}
</script>
</head>
<body data-page="single">
<nav>
<div class="nav-inner">
<a href="/" class="logo">java.<span>evolved</span></a>
<div class="nav-right">
<a href="https://github.com/javaevolved/javaevolved.github.io" target="_blank" rel="noopener" class="github-link" aria-label="View on GitHub">
<svg width="20" height="20" viewBox="0 0 20 20" fill="currentColor">
<circle cx="10" cy="10" r="9" fill="none" stroke="currentColor" stroke-width="1.5"/>
<path d="M10 3C6.13 3 3 6.13 3 10c0 3.09 2 5.71 4.77 6.63.35.06.48-.15.48-.33v-1.16c-1.95.42-2.36-1.07-2.36-1.07-.32-.81-.78-1.03-.78-1.03-.64-.43.05-.42.05-.42.7.05 1.07.72 1.07.72.63 1.08 1.65.77 2.05.59.06-.46.24-.77.44-.95-1.57-.18-3.22-.78-3.22-3.48 0-.77.27-1.4.72-1.89-.07-.18-.31-.9.07-1.87 0 0 .59-.19 1.93.72.56-.16 1.16-.24 1.76-.24s1.2.08 1.76.24c1.34-.91 1.93-.72 1.93-.72.38.97.14 1.69.07 1.87.45.49.72 1.12.72 1.89 0 2.71-1.65 3.3-3.23 3.47.25.22.48.65.48 1.31v1.94c0 .19.13.4.48.33C15 15.71 17 13.09 17 10c0-3.87-3.13-7-7-7z"/>
</svg>
</a>
<a href="/" class="back-link">β All patterns</a>
<div class="nav-arrows">
<a href="/aot-class-preloading.html" aria-label="Previous pattern">β</a>
</div>
</div>
</div>
</nav>
<article class="article">
<div class="breadcrumb">
<a href="/">Home</a>
<span class="sep">/</span>
<a href="/?cat=language">Language</a>
<span class="sep">/</span>
<span>Default interface methods</span>
</div>
<div class="tip-header">
<div class="tip-meta">
<span class="badge language">Language</span>
<span class="badge intermediate">intermediate</span>
</div>
<h1>Default interface methods</h1>
<p>Add method implementations directly in interfaces, enabling multiple inheritance of behavior.</p>
</div>
<section class="compare-section">
<div class="section-label">Code Comparison</div>
<div class="compare-container">
<div class="compare-panel old-panel">
<div class="compare-panel-header">
<span class="compare-tag old">β Java 7</span>
<button class="copy-btn" data-code="old">Copy</button>
</div>
<div class="compare-code">
<pre class="code-text">// Need abstract class to share behavior
public abstract class AbstractLogger {
public void log(String msg) {
System.out.println(
timestamp() + ": " + msg);
}
abstract String timestamp();
}
// Single inheritance only
public class FileLogger
extends AbstractLogger { ... }</pre>
</div>
</div>
<div class="compare-panel modern-panel">
<div class="compare-panel-header">
<span class="compare-tag modern">β Java 8+</span>
<button class="copy-btn" data-code="modern">Copy</button>
</div>
<div class="compare-code">
<pre class="code-text">public interface Logger {
default void log(String msg) {
System.out.println(
timestamp() + ": " + msg);
}
String timestamp();
}
// Multiple interfaces allowed
public class FileLogger
implements Logger, Closeable { ... }</pre>
</div>
</div>
</div>
</section>
<section class="why-section">
<div class="section-label">Why the modern way wins</div>
<div class="why-grid">
<div class="why-card">
<div class="why-icon">π</div>
<h3>Multiple inheritance</h3>
<p>Classes can implement many interfaces with default methods, unlike single abstract class inheritance.</p>
</div>
<div class="why-card">
<div class="why-icon">π¦</div>
<h3>API evolution</h3>
<p>Add new methods to interfaces without breaking existing implementations.</p>
</div>
<div class="why-card">
<div class="why-icon">π§©</div>
<h3>Composable behavior</h3>
<p>Mix and match capabilities from multiple interfaces freely.</p>
</div>
</div>
</section>
<div class="info-grid">
<div class="info-card">
<div class="info-label">Old Approach</div>
<div class="info-value red">Abstract classes for shared behavior</div>
</div>
<div class="info-card">
<div class="info-label">Modern Approach</div>
<div class="info-value green">Default methods on interfaces</div>
</div>
<div class="info-card">
<div class="info-label">Since JDK</div>
<div class="info-value accent">8</div>
</div>
<div class="info-card">
<div class="info-label">Difficulty</div>
<div class="info-value blue">intermediate</div>
</div>
</div>
<section class="bs-section">
<div class="section-label">JDK Support</div>
<div class="bs-card">
<div class="bs-feature-name">Default interface methods</div>
<span class="baseline-badge widely">Available</span>
<p class="bs-desc">Available since JDK 8 (March 2014).</p>
</div>
</section>
<section class="explanation">
<h2>How it works</h2>
<p>Before Java 8, sharing behavior across unrelated classes required abstract classes, which limited you to single inheritance. Default methods let interfaces provide method implementations, so classes can inherit behavior from multiple interfaces. This was essential for evolving the Collections API (e.g., List.forEach, Map.getOrDefault) without breaking existing implementations.</p>
</section>
<section class="related">
<h2>Related patterns</h2>
<div class="related-grid">
<a href="/private-interface-methods.html" class="tip-card">
<div class="tip-card-body">
<div class="tip-card-header">
<div class="tip-badges">
<span class="badge language">Language</span>
<span class="badge intermediate">intermediate</span>
</div>
</div>
<h3>Private interface methods</h3>
</div>
<div class="card-code">
<div class="card-code-layer old-layer">
<div class="mini-label">Java 8</div>
<pre class="code-text">interface Logger {
default void logInfo(String msg) {
System.out.println(
"[INFO] " + timestamp() + msg);
}
default void logWarn(String msg) {
System.out.println(
"[WARN] " + timestamp() + msg);
}
}</pre>
</div>
<div class="card-code-layer modern-layer">
<div class="mini-label">Java 9+</div>
<pre class="code-text">interface Logger {
private String format(String lvl, String msg) {
return "[" + lvl + "] " + timestamp() + msg;
}
default void logInfo(String msg) {
System.out.println(format("INFO", msg));
}
default void logWarn(String msg) {
System.out.println(format("WARN", msg));
}
}</pre>
</div>
<span class="hover-hint">Hover to see modern β</span>
</div>
<div class="tip-card-footer">
<span class="browser-support"><span class="dot"></span>JDK 9+</span>
<span class="arrow-link">β</span>
</div>
</a>
<a href="/pattern-matching-instanceof.html" class="tip-card">
<div class="tip-card-body">
<div class="tip-card-header">
<div class="tip-badges">
<span class="badge language">Language</span>
<span class="badge beginner">beginner</span>
</div>
</div>
<h3>Pattern matching for instanceof</h3>
</div>
<div class="card-code">
<div class="card-code-layer old-layer">
<div class="mini-label">Java 8</div>
<pre class="code-text">if (obj instanceof String) {
String s = (String) obj;
System.out.println(s.length());
}</pre>
</div>
<div class="card-code-layer modern-layer">
<div class="mini-label">Java 16+</div>
<pre class="code-text">if (obj instanceof String s) {
System.out.println(s.length());
}</pre>
</div>
<span class="hover-hint">Hover to see modern β</span>
</div>
<div class="tip-card-footer">
<span class="browser-support"><span class="dot"></span>JDK 16+</span>
<span class="arrow-link">β</span>
</div>
</a>
<a href="/sealed-classes.html" class="tip-card">
<div class="tip-card-body">
<div class="tip-card-header">
<div class="tip-badges">
<span class="badge language">Language</span>
<span class="badge intermediate">intermediate</span>
</div>
</div>
<h3>Sealed classes for type hierarchies</h3>
</div>
<div class="card-code">
<div class="card-code-layer old-layer">
<div class="mini-label">Java 8</div>
<pre class="code-text">// Anyone can extend Shape
public abstract class Shape { }
public class Circle extends Shape { }
public class Rect extends Shape { }
// unknown subclasses possible</pre>
</div>
<div class="card-code-layer modern-layer">
<div class="mini-label">Java 17+</div>
<pre class="code-text">public sealed interface Shape
permits Circle, Rect {}
public record Circle(double r)
implements Shape {}
public record Rect(double w, double h)
implements Shape {}</pre>
</div>
<span class="hover-hint">Hover to see modern β</span>
</div>
<div class="tip-card-footer">
<span class="browser-support"><span class="dot"></span>JDK 17+</span>
<span class="arrow-link">β</span>
</div>
</a>
</div>
</section>
</article>
</section>
<div class="social-share">
<span class="share-label">Share</span>
<a href="https://x.com/intent/tweet?url=https%3A%2F%2Fjavaevolved.github.io%2Fdefault-interface-methods.html&text=Default%20interface%20methods%20%E2%80%93%20java.evolved" target="_blank" rel="noopener" class="share-btn share-x" aria-label="Share on X">π</a>
<a href="https://bsky.app/intent/compose?text=Default%20interface%20methods%20%E2%80%93%20java.evolved%20https%3A%2F%2Fjavaevolved.github.io%2Fdefault-interface-methods.html" target="_blank" rel="noopener" class="share-btn share-bsky" aria-label="Share on Bluesky">π¦</a>
<a href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fjavaevolved.github.io%2Fdefault-interface-methods.html" target="_blank" rel="noopener" class="share-btn share-li" aria-label="Share on LinkedIn">in</a>
<a href="https://www.reddit.com/submit?url=https%3A%2F%2Fjavaevolved.github.io%2Fdefault-interface-methods.html&title=Default%20interface%20methods%20%E2%80%93%20java.evolved" target="_blank" rel="noopener" class="share-btn share-reddit" aria-label="Share on Reddit">⬑</a>
</div>
<footer>
<p>A project by <a href="https://github.com/brunoborges" target="_blank" rel="noopener">Bruno Borges</a></p>
<p><a href="https://github.com/javaevolved/javaevolved.github.io" target="_blank" rel="noopener">View on GitHub</a></p>
</footer>
<script src="app.js"></script>
</body>
</html>