aspose file tools*
The moose likes Java in General and the fly likes Inner and anonymous class method inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inner and anonymous class method inheritance" Watch "Inner and anonymous class method inheritance" New topic
Author

Inner and anonymous class method inheritance

J. Nuno
Greenhorn

Joined: Jul 11, 2008
Posts: 13
Hi,

If I run this the output is My Frog speaking...



If I run this the output is Frog speaking...



And if I run this the output is again My Frog speaking...



I'm lost, can someone explain me this behaviour? Thanks!
Maurizio Nagni
Ranch Hand

Joined: May 29, 2004
Posts: 75
You are right it is a curious situation, but with some logic we can get out of this "paradox".
The private signature forbidden any call outside the class it is defined, that's clear, and that's why anyway you cannot call from your ovrerridden method: because is private, it calls the "speak" method of the class the call is in.
May be to make it more clear try to compare your code with



you will receive an error because speak() is private

To make it worthly work your code should look something like

now the oroginal method is protected, but it print out "My Frog is speaking..." as you expected because the method can be called outside the class it is defined.
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
The explanation could be -
Case 1 : It is normal anonymous class behaviour, so doesnt require explanation.
Case 2 : private methods cannot be overridden. And also note that the reference is actually of a parent Frog. Since the method is private, at run time the child anonymous inner is not even checked for a override.
Case 3 : Here there is no parent reference and it is similar to calling the anonymous child inner method.
Summing up - Its the reference Frog f that is making the difference.


Cheers!
RSR
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner and anonymous class method inheritance