Can an interface can be instantiated in an anonymous class? Does this code have an instance of an interface?
// From Kathy Sierra's book page 471
interface Cookable {
public void cook();
}
class Food {
Cookable c = new Cookable(){ // instantiates an interface
public void cook() {
System.out.println("Anonymous cookable implementer");
}
};
}
public class
Test {
public static void main(
String arg[]){
Food f = new Food();
f.c.cook();
}
}
I bring the question up because of Marcus Green's Mock Exam 2 Question 57.