Reputation: 149
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:
Runnable r = new Runnable() {
// some implementation
}
Upvotes: 14
Views: 67549
Reputation: 31
You cannot create Object of interface, but you can create Object of class that is extends interface.
Like if you like to create an Object of Interface Laptop
and declare method void writeCode()
and implement this method in class class Development
you can use concept of anonymous inner class concept.
now you can create object of anonymous inner class object reference or type of Interface Laptop
public interface Laptop {
public abstract void writeCode();
}
public class Developer implements Laptop{
public void writeCode() {
System.out.println("In developer Class");
}
}
public class Main {
public static void main(String[] args) {
Laptop lap = new Laptop() {
@Override
public void writeCode() {
System.out.println("In new Method");
}
};
lap.writeCode();
}
}
Upvotes: 0
Reputation: 13576
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.
Upvotes: 20
Reputation: 65
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.
Upvotes: 3
Reputation: 7640
What you are seeing is an anonymous inner class.
it’s creating an instance of a new, anonymous,implementer of Runnable class.
Because an anonymous class definition is an expression, it must be part of a statement.
Upvotes: 0
Reputation: 1075915
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.)
Upvotes: 8
Reputation: 9974
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
.
Upvotes: 9
Reputation: 600
we can not instatiate the interface (since do not have constructor).
Upvotes: 0
Reputation: 3630
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.
Upvotes: 13