I’m trying to differentiate between listeners and adapters.
Are they pretty much the same but in listeners you have to implement all the methods in the interface, but with adapters you have an option to only implement the methods you need so the code is cleaners and easier to read?
I also got told that adapters enable instantiation with only one implementation and you can’t instantiate listeners, I don’t fully understand this.
Can someone please explain which one is better to use and things you can do with one but you can’t with the other?
WindowListener is
interfacewhich force you tooverrideall of the methods, while WindowAdapter is implementation ofWindowListenerand you only need tooverridethe method(s) that you interest to deal with.WindowListeneris interface which mean you cant instantiation theWindowListener, whileWindowAdapteris concrete class that you can usenewoperator to instantiation.When you use
WindowAdapter, the code is more clean where your class only override the method(s) that you want.For example:
WindowListener
WindowAdapter
While using adapter the code is cleaner:
Or
So I would recommend to use
WindowAdapter, but not must follow. However, two of the API about the same just thatWindowAdapterexists as convenience for creating listener objects.EDIT:
Since
WindowListenerisinterface, you can implement it at your JFrame subclass.But you cant do it with
WindowAdapter.