Computer Science Canada

Instantiating an interface

Author:  np_123 [ Mon Jan 18, 2016 11:02 pm ]
Post subject:  Instantiating an interface

So I know some stuff about coding in Java, and the basics of how encapsulation and other OOP concepts work, but this code excerpt(not my own) kinda surprised me...
You can't instantiate abstract classes and you can't instantiate regular interfaces so what's so special about a nested/inner interface that allows this code to run? Also, when and why would you instantiate an interface?

Java:

public class Shuffler {

        public interface IShuffle{
                public void shuffle(int[]a);
        }

        public static void main(String[] args) {
                int m=Integer.parseInt("10");
                int n=Integer.parseInt("100000");
                IShuffle shuffle=new IShuffle(){
                        public void shuffle(int[]a){
                                for(int i=0;i<a.length;i++){
                                        int r=i+new Random().nextInt(a.length-i);
                                        int temp=a[i];
                                        a[i]=a[r];
                                        a[r]=temp;
                                }
                        }

        }

}

Author:  Insectoid [ Tue Jan 19, 2016 6:30 pm ]
Post subject:  RE:Instantiating an interface

looks like this has been answered here.

Author:  np_123 [ Tue Jan 19, 2016 6:49 pm ]
Post subject:  RE:Instantiating an interface

hmm, yeah that's interesting. haven't really come across that type of usage before


: