Is it possible to create an object for an interface? If yes, how is it done? According to my view the following code says that we can:
Is it possible to creating object for an interface?
No. The code you've shown creates an object from an anonymous class, which implements the interface. Under the covers, the JVM actually creates a class implementing the interface, and then creates an instance of that class.
The "anonymous" class generated will actually have a name, based on the name of the class in which this code appears, for instance YourClass$1
or similar. E.g.:
public class AnonymousName {
public static final void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
}
};
System.out.println(r.getClass().getName());
}
}
...outputs
AnonymousName$1
(At least on Oracle's JVM; I don't know if the naming convention is in the JLS or if it's JVM-specific behavior.)
This is not creating the instance of Interface, it is creating a class that implements interface. So when you write:
Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
}
};
You are actually a creating a class that is implementing the Runnable interface.
You need to follow all rules here, here, we are overriding the run method for Runnable
. There is similar thing for abstract class also. We can test using an example:
public abstract class AbstractClass {
public void someMethod() {
System.out.println("abstract class");
}
}
and another class i.e. TestClass
:
public class TestClass {
public static void main(String[] args) {
AbstractClass abstractClass = new AbstractClass() {
public void someMethod() {
System.out.println("concrete class method");
}
};
abstractClass.someMethod();
}
}
This will create the instance of a subclass in which we are overriding someMethod()
;
This program prints:
concrete class method
This proves we are creating the instance of subclass.
You can't instantiate an interface directly, but you can instantiate a class that implements that interface:
public class RunClass implements Runnable {
// Class implementation
}
Runnable r = new RunClass();
This is basically the same as what you're doing inline. The brackets after new Runnable() will contain your implementation inline.
You can create an anonymous inner class:
Runnable r = new Runnable() {
@Override
public void run() {
}
};
Therefore you create a new class
which implements the given interface
.
we can not instatiate the interface (since do not have constructor).
Here's my understanding.
An interface
public Interface SomeInterface{
}
You can declare an Object for an interface.
SomeInterface anObject;
You cannot instantiate this object directly using this interface. However, let's say you have a class that implements this interface.
public class SomeClass implements SomeInterface {}
Then you can do this,
anObject = new someClass();
(This is conceptually of course (like pseudocode) actual code may vary depending on your classes and access modifiers etc.)
I'll update as to why exactly we are doing this/what the point is as soon as I find out.
Note: Some of this has been mentioned in above answers, just want the OP to know this whole thing too.