2

What I know about interfaces is that:

1) Interfaces are list of methods i.e Template

2) Interface type can refer to an object of class which implements that interface.

Today I came across below code

interface Test  
{  
  void greet();  
}  
public class Demo  
{  
  public static void main(String[] args)  
  {  
    Test t = new Test()  ---> Is it correct ? WHY ?
     {  
       public void greet()  
       {  
         System.out.print("\nHi, Best wishes to way2java.com\n");  
       }  
    };  
    t.greet();  
    }  
}  

Am not able understand the intention of below code snippet

Test t = new Test() 

1) Do Interfaces have constructors ? My knowledge says Interfaces are having only methods lists but not a definition

2) Is it legal to create a object of Interface ? My knowledge says, the interface type can only refer to an object of class which implements that interface.

3) If its legal then whats the purpose ?

Thanks

LearnJava
  • 372
  • 1
  • 4
  • 16
  • I believe this is the format for an inline implements. Because they are both calling a new instance of it and then implementing the method list defined in it. – fjoe Aug 26 '17 at 13:15

1 Answers1

1

1) No, they don't.

2) No.

What you are creating here is an anonymous class that implements your interface - notice that you were forced to implement the greet method.

There are situations in your code when you simply do not care about giving your implementation a particular name - then you can pass the anonymous inner class - the whole concept of lambda expressions in Java is based on this.

Let's say you want to print the content of a list using the forEach method:

Arrays.asList(1, 2, 3)
  .forEach(...);

The forEach accepts a Consumer instance which is an interface. So, you could either create your own class:

class IntegerPrinter implements Consumer<Integer> {

@Override
public void accept(Integer integer) {
    System.out.println(integer);
}

but that's too trivial to create a new class for it, so it is possible to pass an anonymous instance instead:

Arrays.asList(1, 2, 3)
  .forEach(new Consumer<Integer>() {
      @Override
      public void accept(Integer integer) {
          System.out.println(integer);
      }
  });

A little bit better but still quite verbose. This is where lambda expressions come in handy:

 Arrays.asList(1, 2, 3)
   .forEach(integer -> System.out.println(integer));
Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
  • Anonymous class here you mean, any random class(In this case its Test) which JVM creates if we create an instance like that. In back-end some random class always present which is implementing Test interface. We do it in this way to reduce efforts for creating a new class which is implementing that(Test) interface. Is my understanding correct ? – LearnJava Aug 26 '17 at 13:28
  • 1
    @LearnJava Yes, but at the end of the day, you are implementing it anyway but you are not giving that class a name. – Grzegorz Piwowarek Aug 26 '17 at 13:30