inheritance - Java 8 - Call interface's default method with double colon syntax -


i'm delving java 8 innovations, , i'm trying call default method implement in high-level interface, when subclass overrides it. have no problem going implementing comparator in businesslogic class, wondering if there's magic lets me use nifty new ::.

code:

public interface identity extends comparable<identity> {      int getid();      @override     default int compareto(identity other) {         return getid() - other.getid();     } }  public class game implements identity {     @override     public int compareto(identity o) {         if (o instanceof game) {             game other = (game) o;             int something;             // logic             return something;         }         return super.compareto(o);     } }  public class businesslogic {     private void sortlist(list<? extends identity> list) {         // game, game::compareto gets called want, in call only, default method         collections.sort(list, identity::compareto);     } } 

one way put compare method in static method:

public static interface identity extends comparable<identity> {   int getid();   @override default int compareto(identity other) {     return defaultcompare(this, other);   }   static int defaultcompare(identity first, identity second) {     return first.getid() - second.getid();   } } 

then method be:

collections.sort(list, identity::defaultcompare); 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -