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?
Instantiate an interface
Moderator: admin
-
- Posts: 24
- Joined: Wed Sep 02, 2015 3:43 am
- Contact:
-
- Site Admin
- Posts: 9804
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Instantiate an interface
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.
-Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 1 guest