when we are creating the anonymous class at the method argument
we are directly creating the object of the class
when we are declaring the method
then what do we do here?
it states that the method argument variable f of type Foo is referring to the object that is passed to this method
now here
when we actually pass the argument to the method
we know that the method foof() is of the class (actually anonymous class here ) that implements the interface Foo
now
we know that the method foof() can be called only by using the reference of type
class that implements Foo interface
according to our program only the reference variable f in the method declaration doStuff(Foo f) can refer to such class the implements Foo interface
polymorphism applies here
we are referring to the subclass (in this case the implementing class) object using the super type reference (here the reference is interface type)'
to call the method foof() we can use only the variable f
and also the variable f will be available only for the method doStuff() as that is the method argument
so we call the foof() method in doStuff() method as follows
this should solve the problem
Now let us talk about the things you have done wrong in your program
the code means
1. Create a variable my of type MyWonderfulClass
2. Create a new object of type MyWonderfulClass and call the go method of that class
3. notice here that the go() method returns nothing i.e. the return type of the method is void
4. and using the assignment operator you are trying to assign a void type to MyWonderfulClass type (notice here that null and void are totally different)
this causes the error incompatible types
and also
doStuff() method belongs to the class Bar and you are calling it using the my variable hence the error cannot find symbol as the MyWonderfulClass does not contain doStuff( )method
also
the code
cannot compile because here again you made the same mistake
go() returns void and we cannot call doStuff() method using the void (void cannot be deferenced)