Wednesday, October 28, 2009

OOPS: Creating object for an Interface is Possible

    Use the below sample code to create an object for an interface. This is called as "anonymous inner class".
What's happening here is that an object gets instantiated that implements the interface.

    public interface MyInterface {
    public void myMethod() ;
    }

    ...

    MyInterface myIntfObj = new MyInterface() {
    public void myMethod() {
    ....
    }
    };

    myIntfObj.myMethod();


No comments: