Way2Java

Can you create Interface object?

No, it is not possible. Designers did not provide a way. Of course, it is of common sense also. Because interface contains only abstract methods and as abstract methods do not have a body (of implementation code), we cannot create an object.

Suppose even if it is permitted, what is the use. Calling the abstract method with object does not yield any purpose as no output. No functionality to abstract methods.

Then, what is the use of interfaces in Java design and coding. They can be used as prototypes from which you can develop new classes easily. They work like templates for other classes that implement interface just like a blue print to construct a building.

But, it is possible to create Interface object with inner classes and anonymous objects as in the following code.
interface Test  
{  
  void greet();  
}  
public class Demo  
{  
  public static void main(String[] args)  
  {  
    Test t = new Test()  
     {  
       public void greet()  
       {  
         System.out.print("\nHi, Best wishes to way2java.com\n");  
       }  
    };  
    t.greet();  
    }  
}  



Output screenshot on Interface object

Let us answer one more question.

Can we create an object of abstract class where all methods are non-abstract (concrete)? Answer for this is also "no". The answer for this is already discussed in Can we instantiate an abstract class?

Let us dig more on interfaces to play with. For in and outs of interfaces read: Topics related to In and Outs of interfaces.