Can an interface be instantiated in Java?

No, an interface can not be instantiated in Java. So, if you have an interface called SomeInterface, then the following code will never compile:

SomeInterface s = new SomeInterface( );

However, because an interface is a type, you are allowed to write a method with a parameter of an interface type. And that method parameter will accept – as an argument – any class that implements the interface. If that’s confusing, just take a look at the example below. Suppose we have an interface called SomeInterface, and a method called SomeMethod defined in a class called SomeClass:


public interface SomeInterface 
{
  /* some code here...*?
}

public class SomeClass implements SomeInterface
{
  public void SomeMethod(SomeInterface s )
  {
    /* some code here */    
  }

}

In the code above, you can see that the method SomeMethod will accept an argument of type SomeInterface. And, any class that implements the SomeInterface interface can be passed in as an argument, because of the fact that the class would be of the SomeInterface type.

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

Subscribe to our newsletter for more free interview questions.