Can you instantiate an interface in Java?

Can you instantiate an interface in Java? The answer is no, but you might see some code examples that cause you to scratch your head and question your understanding of the rule. Lets say we have this interface:

public interface MyInterface{
public void myMethod();
}

 

You can actually create an anonymous class that implements this interface like this:

MyInterface myInterface = new MyInterface(){
@Override
public void myMethod(){
//some code goes here...
}
};

 

When I was first learning Java this really confused me. But what's really going on is that you are creating an anonymous inner class that implements the interface. And you can get away with instantiating it like this because you are providing the implementation to satisfy the contract of the interface. Note that the @Override annotation simply tells the compiler to make sure you are using the same signature as the method that is declared in MyInterface.

This pattern is actually very common, and you'll see it a lot in GUI programming, and you'll also see it in Android quite a bit.

I got confused because I did not see the 'implements' keyword anywhere. I would have guessed that the syntax for doing this would look something like this, but this goofy because it's not really an anonymous class:

MyInterface myInterface = new Blah implements MyInterface(){
public void myMethod(){
//some code goes here...
}
};

 

Note that anonymous inner classes can either extend a class OR can implement only one interface. Here's an example of an anonymous class that extends a class (notice that it does not include the 'extends' keyword):

BaseClass baseClass = new BaseClass(){
//code goes here...
};

 

 

 

 

Add a Comment...

 

Comments

Wow, that's what I was looking for, just what a information! present at this webpage, thanks admin with this website. - 9/28/2016

Howdy! Someone inside my Facebook group shared this site around so I came to give it a look. I'm definitely loving the data. I'm bookmarking and are tweeting this to my followers! Great blog and great style and design. - 9/18/2016

Heya i am initially here. I discovered this board and I find It truly useful & it helped me to out much. I am hoping to provide something back and help others as if you aided me. - 9/14/2016

Excellent article! We will be linking for this particularly great post on our website. Keep up the good writing. - 9/06/2016

There's certainly much to find out about this topic. I enjoy every one of the points you may have made. - 9/03/2016

СмоÑÑиÑе лÑÑÑе здеÑÑ: ландÑаÑÑнÑй дизайн (Dani) https://goo.gl/C46i8W - 5/18/2017

СмоÑÑиÑе лÑÑÑе здеÑÑ: ландÑаÑÑнÑй дизайн https://goo.gl/D56XLP - 5/18/2017

sharesocial runbusking doctorbridge rockndata askcar storagesolutions-selfstorage this-is-not-normal sireofforfoundation in2040 runbusking - 5/09/2017

RickySupp Spanien AracelyAn MarcoMaru Tjeck IlseCohen ZDWMauric Manchester United Micheline SantosDnt Chelsea YoungRoxo DevonLasl Atletico Madrid NannieMon AntjeCarm Kroatien LatishaEa Margarita Italien TheresaHu LowellPar Schweiz GemmaWeat Roosevelt Tottenham Hotspurs MarianneP FlossieBo Polen RacheleBa - 3/07/2017

Fastidious response in turn of this difficulty with firm arguments and telling everything on the subject of that. - 11/23/2016