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.

Example of anonymous inner class implementing an interface

interface ProgrammerInterview  {

public void read();

}

class Website  {
ProgrammerInterview p = new ProgrammerInterview () {
public void read() {
System.out.println("interface ProgrammerInterview 
anonymous class implementer");
}
};
}

An explanation of our example anonymous inner class implementing an interface

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:

/*code to implement the ProgrammerInterview interface:*/
class SomeClass implements ProgrammerInterview //...

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:

ProgrammerInterview p = new ProgrammerInterview () { 
//...
};

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

FOLLOW Varoon Sahgal, Author of ProgrammerInterviewon
  • annonymousProgrammer

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

    • Arun K

      In reality it is not instantiating the interface. It is providing an implementation to the interface and initializing it. I will try to explain.

      In typical scenarios your code might be like this (syntax is not exact)
      ——
      public interface HelloName{
      public String sayName(String s)
      }
      ——
      public class HelloFirstName implements HelloName{
      public String sayName(String s){
      return (“Hello First Name + s )
      }}
      —–
      public class HelloLasttName implements HelloName{
      public String sayName(String s){
      return (“Hello Last Name + s )
      }}

      public class Client{
      public static void main {
      HelloName h= new HelloFirstName ()
      syso ( h.sayName (“Tom”) )
      }}
      ———————————–

      for simple interfaces you can use anonymous class to provide implementation. It is like providing an implementation class with no name and initializing it. See example below

      ————————————
      public interface HelloName{
      public String sayName(String s)
      }
      ——

      public class Client{
      public static void main {
      HelloName h= new HelloName{ public String sayName(String s){return (“Hello First Name + s )}}
      syso (h.sayHello(“Tom”)
      }}

  • 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!

  • nastika

    Thank you! The explanation made everything a lot more clear :)

  • veer

    Good …

    • xjava

      very nice explanation