Originally posted by James Tharakan:
As far as understood ,anonymous class EXTENDS a class.
b=(B)new Serializable(){};
So here you are trying to extend a interface(Serializable).
Somebody correct me if i am wrong.
Sorry James, you are wrong. Anonymous inner classes can be used to both 'extends' and 'implements' tasks.
The problem is with the "reference type" of an anonymous inner class. Look at that anonymous inner class declaration fragment:
new Serializable(){} How do you think about the above inner class expression? Do you think it is same as to, something like:
Serializable innerClass = new Anonymouse(); No! But actually it is same as to:
Anonymous innerClass = new Anonymouse(); (where Anonymous is something that implements the Serializable interface)
Now consider the following program:
Both of the above two statements gives you a compile time error, because of the SAME reason. Try to understand through the above program.