Tuesday, August 21, 2018

Java8 - Learn Interface Default and Static Methods tutorials with examples

In this blog post, We will cover the basics of Default methods interfaces tutorials with examples.

Why Default Methods is required?

Java8 Default methods, static methods
Default Methods is one of the features introduced for java8 version. Prior to Java 8 version, Interface has only abstract methods. Implementations of abstract methods have to be provided by the class which implements the interface. If any interface code is released, We can't change the interface structure, if any changes required for the interface, It causes code breaking in classes which implements interfaces.
See the Interface declaration
class ClassTwo implements MyInterface {
 public void methodone() {
 }
}
interface MyInterface {
 public void methodone();
}
In the above example, Declared a class by implementing MyInterface. This works perfectly. Now add one more method - methodtwo in an interface like below
interface MyInterface {
 public void methodone();
 public void methodtwo();

}
ClassTwo gives compilation error and breaks the functionality. The type ClassTwo must implement the inherited abstract method MyInterface.methodtwo() You have to implement a new method. Forcing clients to extend their functionality with interface changes. This is a contradiction to class design. ClassTwo compilation successfully when you implemented the new method.

To avoid this type of design problems, the Default method is introduced. Default methods added extra functionality to interface to without breaking the structure of classes that implement it. Default methods are backward compatible to the previous version.

Interface Default Methods Example

methodDefault() is default method in InterfaceDemo. Added extra functionality to interfaces without breaking the structuring of implementation classes.
public class MyClass implements InterfaceDemo {
 @Override
 public void methodHeader() {
  System.out.println("methodHeader Implementation");
 }
 public static void main(String[] args) {
  MyClass myClassObject = new MyClass();

  // call interfaec abstract method
  myClassObject.methodDefault();
  // Call Interface abstract method
  myClassObject.methodHeader();
 }
}
interface InterfaceDemo {
 // This is default method
 default void methodDefault() {
  System.out.println("Default method");
 }
 // This is abstract method
 void methodHeader();
}
Output is
Default method
methodHeader Implementation

Default Method Inheritance

interface InterfaceOne {
    default void method(){
       System.out.println("InterfaceOne.method()");
    }
}

interface InterfaceTwo {
    default void method(){
       System.out.println("InterfaceTwo.method()");
    }
}

public class ClassOne implements InterfaceOne, InterfaceTwo{
}
The code throws compilation error as lie Duplicate default methods named a method with the parameters () and () are inherited from the types InterfaceTwo and InterfaceOne.
Fix for duplicate default named method is to override default method as like below


public class ClassOne implements InterfaceOne, InterfaceTwo{
    public void method(){
        System.out.println("class method");
     }
}
To call the default method of InterfaceOne, use the super keyword with method name in the implemented method.


public void method(){
     InterfaceOne.super.method();    
    
    }
 

Interface Static Method example

 Static methods are similar to default methods in interfaces. They add extra functionality in the interface without breaking code in implementation classes. a static method cannot override in implementation classes.
public class MyClass implements InterfaeDemo {
 @Override
 public void methodHeader() {
  System.out.println("methodHeader Implementation");
 }
 public static void main(String[] args) {
  MyClass myClassObject = new MyClass();

  // call interfaec abstract method
  myClassObject.methodDefault();

  // Call Interface abstract method
  myClassObject.methodHeader();
  // interface static method is calling
  InterfaeDemo.methodStatic();
 }

}
interface InterfaeDemo {
 // This is default method
 default void methodDefault() {
  System.out.println("Default Method");
 }
 static void methodStatic() {
  System.out.println("Static Method");
 }
 // This is abstract method
 void methodHeader();
}
Output is
Default Method
methodHeader Implementation
Static Method

Difference between Abstract methods and default interface methods in java8? 

The interface has now Default methods with the body. Abstract has concrete methods. But both are not same. An abstract class has a constructor, the interface doesn't have a constructor. Abstract classes define structured state with them. Interface added default implementation. Both are defined for a different purpose.

Difference between Default methods and static methods in an interface? 

A static method cannot be overridden in implementation classes Default method can be overridden in the implementation classes Static method can be accessed by Interface in implementation class since these belong to Interfaces. Default methods can be accessed by objects of implementation class Class And interface contains same static method names, but they are not overriding each other.

Related article


EmoticonEmoticon