your anonymous class is actually a sub-class of class Popcorn
So your reference variable p is actually referring NOT to an instance of Popcorn, but to an instance of the anonymous class!!
So if you want to call the method pop() of the anonymous class from within the class that it is defined in (Food) then you would simply call it like so..
p.pop()
Anonymous classes are used to override methods of the superclass..
Thus, only methods ALSO defined in the super class can be invoked. Even if u define new methods in your anonymous class...you will not be able to call them since you will have to use the reference variable (p in this case) which is of type Popcorn(a parent class) (Ref
olymorphism)
you cannot create new instances of the anonymous class...They can have only one instance ..the one tht is created where u actually define your anonymous class.
So if you want to use your anonymous class method which is overrides the parent class method then you have to use the reference variable (p in this case)
However if you wanna call the anonymous class method from outside the enclosing class(Food)
thn you would have to use the following syntax..
f.p.pop()
as suggested by Ann.