I was trying out inner classes demo when I came around this piece of code.
I am defining "eat()" method in outer as well as in anonymous class. when I try to call obj.eat() it call eat() method of the anonymous class always. What can be done to call eat() method of AnonymousClsDemo.
In your code, the object referenced by obj is an instance of a subclass of AnonymousClsDemo. This subclass (an anonymous class i.e. a class with no name) overrides (replaces) the public void eat() method inherited from its superclass AnonymousClsDemo.
You can create an instance of AnonymousClsDemo and call its eat() method as follows.
Hi Shreyas, I think the answer is in your question itself "What can be done to call eat() method of AnonymousClsDemo?". Well.. how about creating a new object of this class AnonymousClsDemo and calling its eat method directly?
Edit- Just saw Joe's answer, I am saying the same thing as him.