I am learning java swing and came across the concept of anonymous inner class. After studying a bit at this link, I feel that an anonymous inner class must always extend some other class since the method in which it gets passed as a parameter is predefined. At most, if a method accepts Object instances as parameter, only then we can create an anonymous independent inner class. I just seek your opinion whether I am right. It would be helpful if you could provide some example.
Thanks in advance.
I am learning java swing and came across the concept of anonymous inner class.
Share
Creating an anonymous inner class without extending anything would basically mean that you’d have no way of talking to it.
You need to extend a class or implement an interface just to give your object known methods to talk to, for example;
The only way to talk to an instance of that object is through the known ActionListener methods (which happens to be exactly the methods addActionListener needs), any other methods you’d add would only be accessible inside the object itself. If you didn’t have to extend/implement anything, no methods would be externally accessible at all.