Image by Cristina Morillo

Java 8: Default & Static Interface Methods - Similar, but different!

Martin Ombura Jr.
Martin Ombura Jr.
Published in
5 min readJan 15, 2018

--

So Java 8 has officially been out for almost 4 years now, and with it came a variety of cool stuff. Some were improvements to the language like Parallel array sorting and more efficient collision resolution in HashMaps. New features included the ever so popular Lambdas and Method Parameter Reflection (For those bad-ass developers). One new feature in particular is the ability to have Default and Static Interface Methods (Static interface methods were actually included in previous versions of Java, but we shall include them for comparison). This article aims to explain what they are with using a variety of examples.

1. Intro to Default & Static Interface Methods

In EC -1(example code 1, shown below), we see a simple interface declaration. The two methods within the interface perform the exact same operation, however one is default and the other is static.

//EC-1: Intro to default and static interface methods
public interface Foo {
//Default method declaration
default int sumDefault(int a, int b) {
return a + b; //simply adds a + b
}
//Static method declaration
static int sumStatic(int a, int b) {
return a + b; //simply adds a + b
}
}

--

--