minte9
LearnRemember



Default

Java 8 changes required the introduction of default methods in interfaces.
 
/**
 * One of the biggest changes in Java 8 is to collections library.
 * 
 * It required the introduction of a new concept, ...
 * default methods in interfaces.
 * 
 * A dfault methods allows body implementations.
 * Classes implementations win over default methods.
 */

package com.minte9.lambdas.default_methods;

public class DefaultMethod {
    public static void main(String[] args) {
        
        new A().hello(); // Hello default
        new B().hello(); // Hello B
    }
}

interface MyInterface {
    public default void hello() {
        System.out.println("Hello default");
    }
}

class A implements MyInterface { // Look Here
    // no implementation
}

class B implements MyInterface {
    @Override public void hello() {
        System.out.println("Hello B");
    }
}

Functional

Default methods can be used in lambda expresions.
 
/**
 * Default methods can be used in lambda expresions.
 * Functional interface must contain only one abstract method.
 */

package com.minte9.lambdas.default_methods;
import java.util.Arrays;
import java.util.List;

public class Functional {
  public static void main(String[] args) {
      
    List<MyValue> myList = Arrays.asList(
      () -> 1, 
      () -> 3,
      () -> 2 // lambda: get()
    );

    int maxDoubled = myList.stream()
      .mapToInt(x -> x.getDouble()) // lambda: default method
      .max()
      .orElse(0)
    ;

    System.out.println(maxDoubled); // 6
  }
}

@FunctionalInterface interface MyValue {

  int get(); // abstract
  
  default int getDouble() {
    return get() * 2;
  }
}

Object

Default methods can be accessed only with object references!
 
/**
 * Calling a default method in lambas without object reference ...
 * doesn't work!
 */

package com.minte9.lambdas.default_methods;

import static org.junit.Assert.assertEquals;

public class Object {
  public static void main(String[] args) {
      
    Formula a = x -> x * 2; 
    assertEquals(4, a.calculate(2)); 
      // pass

    Formula b = x -> a.sqrt(x); // Look Here
    assertEquals(4, b.calculate(16)); 
      // pass

    //Formula c = x -> sqrt(x); // Not working
  }
}

@FunctionalInterface interface Formula {

  abstract int calculate(int x);

  default int sqrt(int x) {
    return (int) Math.sqrt(x);
  }
}

Super

Be careful when using multiple implementations and default method.
 
/**
 * Interfaces are subject to multiple inheritance.
 * Java compiler might not know which method to choose.
 * 
 * In this case you must override the defaul method ...
 * and provide the choosen interface method
 */

package com.minte9.lambdas.default_methods;

public class Super {
    public static void main(String[] args) {
  
        String msg = new myClass().hello();
        System.out.println(msg); 
            // Hello A
    }
  }
  
  interface AI {
    public default String hello() { return "Hello A"; }
  }
  
  interface BI {
    public default String hello() { return "Hello B"; }
  }
  
  class myClass implements AI, BI {

    @Override public String hello() {
        
        return AI.super.hello(); // Look Here
    }
  }



  Last update: 303 days ago