Oracle Certified Associate: Java 8 default and static methods in interface


We all know about the default access modifier in Java. From Java 8, we can add default method(s) to an interface. The default methods are non abstract method which have implementation i.e. to say have a method body. A part from default, Java 8 also supports static method.

Wait a minute, but we know that interface can only have abstract methods only. The concept of default method is added for backward compatibility with older version of interfaces.
NOTE : default methods and default access specifiers are two different concepts. Not to be confused as same. default method is added from Java 8 where as default access specifier concept is old and still valid in terms of variables and methods.
Think of a situation that we want to add the extra method to the interface, which means all the classes implementing the interface need to be modified to accommodate the new method which make task of adding the new methods to interface difficult. This is where default methods come in to role allowing to add new method with default implementation ensuring the old code doesn’t break or no need to be modified.
Next is how to create default method(s) in interface, very simple just add default keyword in front of method for example
public interface Demo
{
    int add(int a, int b);
    public default int newmethod()
    {
          return 82;
    }
}
We can see interface has two methods i.e. add and newmethod. Add is abstract method as it should be in interface. newmethod is default method with implementation or body. Any class thatimplements the Demo interface needs to provide implementation of add method only as newmethod has default behavior that returns 82. Yes class can override the method but its not compulsion now, and same time the interface change overtime without worrying for the old versions of interface that had been implemented, ensuring the backward compatibility also.
The default method concept raises a question what if two interfaces have same default method, in that case which method will be called, lets look at one example to understand the issue
interface Demo
{
int add(int a, int b);
public default int newmethod()
{
return 82;
}
}
interface NewDemo
{
public default int newmethod()
{
return 29;
}
}
class MyClass implements Demo, NewDemo
{
public int add(int x, int y)
{
return x+y;
}
}
When we call newmethod on MyClass which method will be called ? That is the problem we are talking about with multiple inheritance which was not an issue till now due to all methods are abstract in interface.
The above example will not compile and will throw error like
error: class MyClass inherits unrelated defaults for newmethod() from types Demo and NewDemo
class MyClass implements Demo, NewDemo
^
This is an exception or issue or problem in default method. The solution is to override the newmethod in MyClass so the problem get resolved.
class MyClass implements Demo, NewDemo
{
public int add(int x, int y)
{
return x+y;
}
public int newmethod()
{
return 11;
}
}
So when the newmethod is called on MyClass it will be the local method that will be called and hence the problem of interface default method is resolved by overriding such method.
Let us move to static methods in interface. static methods are methods with static keyword and are associated with the interface only. It is never inherited and can be called using interface name only, not with class or its instance. Following example will clear it more.

interface Demo
{
int add(int a, int b);
public default int newmethod()
{
return 82;
}
static void newstatic()
{
System.out.println(“This is static method of interface”);
}
}
class MyClass implements Demo
{
public int add(int x, int y)
{
return x+y;
}
public int newmethod()
{
return 11;
}
}
class Test
{
public static void main(String args[])
{
Demo.newstatic();
}
}

Calling newstatic method can be done using interface reference i.e. Demo.newstatic(). Using MyClass.newstatic() will throw compile time error “Cannot find symbol”. So static methods are methods of interface which are never inherited. It makes easier to organize helper method in interface only.

Leave a comment

Your email address will not be published. Required fields are marked *