SHARE
TWEET

Default methods and Lambda

votez Jun 19th, 2017 51 in 8 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by votez on 19.6.2017.
  3.  */
  4. public class LambdaDefault {
  5.  
  6.     @FunctionalInterface
  7.     public interface Num {
  8.         int doubleSquare(int i);
  9.  
  10.         default int square(int i){
  11.             return i * i;
  12.         }
  13.     }
  14.  
  15.     public static void main(String[] args) {
  16.         Num n = new Num() {
  17.             @Override
  18.             public int doubleSquare(int i) {
  19.                 return square(i) * 2;   // can use default method!
  20.             }
  21.         };
  22.  
  23.         n.doubleSquare(2);
  24.         Num l = (i) -> square(i) *2;    // compilation error, can not use default method!
  25.         Num k = (i) -> i * i *2;        // works!
  26.     }
  27. }
RAW Paste Data
Want to get better at Java?
Learn to code Java in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top