Java 8 : Static & Default methods

Sarath Manchu
Sep 28, 2016 · 3 min read

There has been few modifications to interface in Java8 ,these changes include static methods & default methods.Untill java7 ,interfaces could have only method declarations.From java8 Interfaces can have Default & Static methods.

Prior to java8 If we want to add new methods to existing interface,it requires modifications in all implementing classes.else It will break the functionality.

Coutesy : Giphy

There are few roundabout solutions to handle this problem,Lets see how java8 designers tackles it.

Default Method:

Default method enable us to add new methods to interface without breaking the functionality of depending classes. i.e we can have non abstract methods in interface with the keyword “default”.

interface example1{
public void method1();
default public void newDefaultMethod() {
System.out.print("Default method added in interface");
}
}

As newDefaultMethod() is default,it’s not mandatory to override in classes which implement interface.

Thats’s All,there is a catch!

As you aware java doesn’t allow multiple inheritance via classes due to famous “Diamond Problem”.where compiler gets ambiguity about parent class method resolving.

With Addition of default methods to interface,same problem would arise for interface as well ,if two interfaces provide same default method.

Courtesy : Giphy

Here is the code snippet illustrating the problem.

interface example1{
public void method1();
default public void defaultMethod() {
System.out.print("Default method added in interface1");
}
}
interface example2{
public void method2();
default public void defaultMethod() {
System.out.print("Default method added in interface2");
}
}

Solution : if any class extending these two interfaces which has same default method, class must ovverride default methods to avoid compile error.

class A implments example1,example2{
@override
public void method1(){
}
@override
public void method2(){
}
@override
default public void defaultMethod() {
System.out.print("Default method must be overridden");
}
}

Static Methods

Static methods are also allowed in java8 interfaces,so far public static final fields i.e constants were only allowed

Static methods are also similar to Default methods except these can’t be overriden by implemented classes.which is same as any static method in normal classes.

interface interface1{
static void staticMethod() {
System.out.print("Static method added in interface1");
}

Static method can only be called through the interface or implementing class type refernce,not with an instance variable.

interface interface1{
static void staticMethod() {
System.out.print("Static method added in interface1");
}
}
class implclass imlements interface1{
public static void main(String args[]){
//legal calls,with class name
interface1.staticMethod():
implclass.staticMethod();
interface1 obj=new implclass(); //Compile error,with instance
obj.staticMethod();
}
}

Food For Thought: interface vs abstract class

With addition of these new methods interface is starting to sound like a abstract class ,are both same?

Courtesy : Giphy

These are similar but abstract class can contain instance variables,interfaces can only have methods and constants.Another difference is,in java class can’t extend mutiple classes,but can implement multiple interfaces.

There is a subtle difference between abstract class & interface is mutability.if you need mutable state prefer abstract class,otherwise prefer interface.

Please checkout this post about Streams, which is the most appealing topic in java8.

Thanks for reading this article. Be sure to click ❤ if you found it helpful. It means a lot to me.

Sarath Manchu

Written by

Lead Android Developer,Puzzler & Avid Reader.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade