Can anybody explain the working of following code...?

Posted by Siddhi on Stack Overflow See other posts from Stack Overflow or by Siddhi
Published on 2010-06-05T02:10:54Z Indexed on 2010/06/05 8:32 UTC
Read the original article Hit count: 150

Filed under:

Can anybody explain the working of following code...?

interface myInterface{}

public class Main {

    public static void main(String[] args) {

        System.out.println(new myInterface(){public String toString(){return "myInterfacetoString";}});

        System.out.println(new myInterface(){public String myFunction(){return "myInterfacemyFunction";}});
    }
}

Output is...

myInterfacetoString
primitivedemo.Main$2@9304b1

All answers saying that myInterface in println() statement is anonymous class. But as I already declared it as an interface, why does it allow me to create anonymous class of same name....?

again...if these are anonymous classes then class main should allow me to give any name to these anonymous classes..But if try to do so..I'm getting compilation error

© Stack Overflow or respective owner

Related posts about java