Newsletter Subscription

Email

Country

Jobboard

Provide an example of an anonymous class that implements an interface

If you already read our first article that included an example of an anonymous class in Java, then you know that the correct terminology is anonymous inner class and not anonymous class – there’s no such thing as anonymous inner classes without the “inner”.

Now that we clarified this point, let’s go through an example and a brief tutorial on an anonymous inner class that actually implements an interface. You can start out by taking a look at the code below.

In the code above, we have an interface called ProgrammerInterview that has just one method declaration inside it. And, we have another class called Website.

Inside the Website class, the code in red is actually creating an instance of an anonymous inner class that implements the ProgrammerInterview interface. Read that sentence again, carefully, to make sure you understand it. The class that is being instantiated (where “p” is the instance variable) is anonymous because it has no name, and the anonymous class is actually implementing the ProgrammerInterview interface. This means that we are simultaneously creating an anonymous class that implements the ProgrammerInterview interface and also creating an instance of that anonymous class. Confusing right? Think about it for a little while until it is clear.

So, you are probably confused because you are used to seeing classes that implement interfaces using the “implements” keyword syntax so this code is what is familiar to you:

But, this is the only time in Java when you will see the syntax like what we showed above – where it looks like we are actually creating an instance of an interface class directly without even using the “implements” keyword. Remember that ProgrammerInterview is still an interface and not a normal class – even though we have the very odd-looking (for an interface) code below:

As I know that interface can’t be instantiated. But you have done this. can u make me clear?

Annis Monadjem

I’m beginning in java and in programming. I have read your well edited articles on ‘anonymous inner classes’, ‘nested classes’ and ‘inner classes’ but still cannot understand one thing. What you really mean by the word ‘inner’ in ‘anonymous inner class’ ? I don’t have a problem understanding the ‘anonymous’ part; what i cannot understand is why you call it ‘inner class’? After all an ‘inner class’ is a kind of ‘nested class’ that is non-static. So when you say ‘anonymous inner class’ I would expect to see some type of ‘nested’ or ‘inner’ class within an enclosing class. I’m even more confused by the syntax that creates an ‘anonymous inner class’ where the name of the ‘super class’ or ‘interface’ is listed, but again there is no reference, whatsoever, to where is the ‘nested class’ and where is the enclosing class (one class would have to physically be enclosed within an enclosing class in the source code). So my guess is that perhaps, as the word ‘anonymous’ suggests, an invisible ‘inner class’ is created under the enclosing class (and which simultaneously is either a subclass of still another class or an implementation of some interface). So referring to your example above, am I correctly understanding that variable ‘p’ is type ‘ProgrammerInterview’, and an instance of an ‘invisible’ and ‘nested’ class nested under ‘Website’ ? (please answer)

shiva kumar

what a explanation and one more thing is annonimass class explanation with out using implement keyword

Mirsa Mohammed

Great explanation.

Thank you.

S.S.H

i just got the deep of this topic in a very simple way …great explanation

Sara

Thank you very much, very well explained. so easy to get 🙂 it almost made my day

letsdecode

Very nice and precise. Thanks!

xjava

very nice explanation

nastika

Thank you! The explanation made everything a lot more clear 🙂

veer

Good …

Would you like to thank ProgrammerInterview.com for being a helpful free resource? Then why not tell a friend about us, or simply add a link to this page from your webpage using the HTML below.

Link to this page:

Please bookmark with social media, your votes are noticed and appreciated: