I am studying for the
SCJP. This code is from
Java 2 Sun Certified Programmer & Developer for Java 2, Authors: Kathy Sierra, Bert Bates
My question is: I understand why I can't access the sizzle() through the Popcorn object. I am trying to figure out how I would go about accessing the sizzle() from the anonymous class.
class Popcorn {
public void pop() {
System.out.println("popcorn");
}
}
class Food {
Popcorn p =new Popcorn() {
public void sizzle() {
System.out.println("anonymous sizzling popcorn");
}
public void pop() {
System.out.println("anonymous popcorn");
}
};
public void popIt() {
p.pop();
//p.sizzle();
//Not legal! Popcorn does not have a sizzle()
}
}