Page 1 of 1

Instantiate an interface

Posted: Sun Sep 27, 2015 11:11 am
by heleneshaikh
I hope it's okay that I post this question in the General Java Discussion...
Theoretically speaking, interfaces cannot be instantiated, except if you override all of the interface's abstract methods like so:
public static void main(String[] args) {
List al = new List() {
@Override
public int size() {
}

@Override
public boolean isEmpty() {
}

@Override
public boolean contains(Object o) {
}
etc...
Theoretically, we cannot instantiate an interface, but in practice you actually can. If the exam asks a question like An interface cannot be instantiated. Should we then answer true or false?

Re: Instantiate an interface

Posted: Sun Sep 27, 2015 11:39 pm
by admin
The answer is true. You cannot instantiate an interface - neither theoretically nor practically. What you are doing is instantiating an anonymous class that implements the interface.
-Paul.