Reputation: 2912
I went to an interview. Interviewer asked me if one can instantiate an interface and abstract class? As per my knowledge I said "No". But he said "Yes, we can with the help of an anonymous class".
Can you please explain to me how?
Upvotes: 2
Views: 2107
Reputation: 347
Yes, we can create by having defining the abstract methods or the interface methods on the fly during instantiation. That's like a Named anonymous class.
//interface
Runnable r = new Runnable(){
public void run() {
System.out.println("Here we go");
}
};
//Abstract class
abstract class MyAbstract {
abstract void run();
}
MyAbstract ab = new MyAbstract(){
@Override
void run() {
System.out.println("Here we go");
}};
Upvotes: 0
Reputation: 861
You cannot create instances of abstract classes or interfaces using the new
operator. For example,
new AbstractSet(); // That's wrong.
You can, however, use them to declare reference variables. For example, You can do this:
AbstractSet set;
You can instantiate anonymous as well as declared implementing classes or subclass.
For example, Set
extends AbstractSet
, so you can instantiate Set
.
Upvotes: 0
Reputation: 124646
I don't know what is "instantiation of interface and abstract class". I think it's an inaccurate, improper expression of something, we can only guess at the intended meaning.
You cannot create an instance of an interface or an abstract class in Java.
But you can create anonymous classes that implement an interface or an abstract class. These won't be instances of the interface or the abstract class. They will be instance of the anonymous class.
Here's an example iterator from the Iterator interface that gives you an infinity of "not really":
new Iterator<String>() {
@Override
public boolean hasNext() {
return true;
}
@Override
public String next() {
return "not really";
}
};
Or a funky AbstractList
that contains 5 "not really":
List<String> list = new AbstractList<String>() {
@Override
public int size() {
return 5;
}
@Override
public String get(int index) {
return "yes";
}
};
Upvotes: 3
Reputation: 3785
Interface :
interface Interface1 {
public void m1();
}
When you right
new Interface1() {
public void m1() {
}
}
Its not actually creating the instance of Interface. Its creating an instance of its subtype which doesnt have any name/reference. Hence we cannot create an instance of interface or abstract class
Upvotes: 0
Reputation:
Assume you have an abstract class: MyAbstractClass
with abstract void method myAbstractMethod
. Then you can make an "instance" of this class via this code:
MyAbstractClass myAbstractClassInstance = new MyAbstractClass() {
public void myAbstractMethod() {
// add abstract method implementation here
}
};
myAbstractClassInstance
extends your MyAbstractClass
in this case. When you instantiate this class you have to implement all abstract methods as you can see from the code above.
The same way works for interfaces, assume you have an interface MyInterface
with a void method myInterfaceMethod
inside, then you can create an "instance" (implementation of this instance) via this code:
MyInterface myInterfaceImpl = new MyInterface() {
public void myInterfaceMethod() {
// add method implementation here
}
}
myInterfaceImpl
is an implemetation of MyInterface
in this case. When you create an object using interface, you have to implement interface methods as it is shown above.
Upvotes: 0
Reputation: 2284
This was a trick questions.
No you can not instantiate an interface or abstract class.
But you can instantiate an anonymous class that implements/extends the interface or abstract class without defining a class object. But it is just a shortcut to defining a fully named class.
So I would say technically your answer was correct.
Upvotes: 5