Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
You won't be able to call it with the p reference. Since sizzle() is not defined in Popcorn, you can't call it on an instance of the anonymous inner class.
Note that the only way to access an "anonymous inner class" is through the reference of the super class (Popcorn in the current case.) Since Popcorn class doesnot know "sizzle" method, there is no way you can call sizzle method on the anonymous inner class just created.
Note: Please do not tell you are getting an error like the one below >>>Food.java:23: cannot resolve symbol symbol : class Food$1 location: class Food perchance you meant 'Food.1' ((Food$1)p).sizzle(); ^ 1 error > Terminated with exit code 1.
--- I am sure my code is correct will work. Well! if you found the answer share it in the forum.
Happy Homeworking [ September 14, 2006: Message edited by: febians sh ]
First overridding means, you are defining a method which is of same signature of your parent method. In your Anon. Class the sizzle() is not an overriding method and you are assiging it to Pop type, so you can't access it.
Remember:
1) Reference type super class can't access, new methods of its subclasses. 2) Reference type attributs always referes to Class type of reference. 3) Reference type methods alwyas referes to Reference type class even type is of Super Class.
I know they are confusing, but just read three times.
"febians sh", please read our JavaRanch Naming Policy and change your displayed name to comply with it. The correct format for a displayed name is <first name or initials for first names><space><family name> and we prefer it if you use your real names.
Your first solution is a hack - you should not require a successful compilation without the hack so as to get the hack to compile. Also it goes against Sun's recommendations to use knowlege of anonymous classes generated by the compiler.
The second solution using Reflection is correct but that is beyond the scope of SCJP.
For SCJP examination purposes the answer is "you cannot call sizzle() through the reference to the anonymous class". [ September 14, 2006: Message edited by: Barry Gaunt ]