Can we create object for interface in java ?

 
We cant create object without method implementations. Anonymous inner classes allow you to do the following.
 

interface TestA {
public void myMethod() ;   
}

TestA  myIntfObj = new TestA() {   
    public void myMethod() {   
    }   
};  

myIntfObj.myMethod();  

 
object gets instantiated that implements the interface.you are converting the interface to an inner class, which you haven’t given a name to (hence anonymous). You have to implement all the interface’s methods, then you have a class, then you create an object from it all in one statement.

 
Recommended Article

 

Technically each class has a name, although for anonymous classes that name is not known at compile time. You can see those classes in the same folder as your other class files.
 

About Gokul


I am very much interested in android and Wicket framework. A core android developer and working in android native app development and responsive web mobile design. I have also worked in wicket fame work and java web development. I will keep on updating you about android and wicket framework and answer your query.

  • guarav

    nyc thnxx :D