Instantiate an interface

Moderator: admin

Post Reply
heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

Instantiate an interface

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Instantiate an interface

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests