+92 332 4229 857 99ProjectIdeas@Gmail.com

Anonymous Inner Classes In Java

Anonymous Inner Classes:

-> Has no name.
-> Same as inner class in capabilities.
-> Much shorter.
-> Difficult to understand.


This following code shows you.. when user close the windows then at closing shows you a message "Good Bye" to you:

        myFrame.addWindowListener(new WindowAdapter() {

                public void windowClosing(WindowEvent we) {

                        JOptionPane.showMessageDialog(null, "GoodBye");





This following code shows you.. when user click the button then shows you a message "Hello World!!!!" to you:

     startButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent ae) {

                        JOptionPane.showMessageDialog(null, "Hello World!!!!");


Named Inner Classes:

-> A class defined inside another class.
-> Inner class can access the instance variables and members of outer class.
-> It can have constructors, instance variables and methods, just like a regular class.
-> Generally used as a private utility class which does not need to be seen by other classes.

public class EventExample {


         // inner class

           private class WindowHandler extends WindowAdapter{

                  // Event Handler for WindowListener     

                  public void windowClosing (WindowEvent we) {


                        JOptionPane.showMessageDialog(null, "Good Bye");          





Note: For more information about events see “Event handling” section

Adapter Classes:

-> For listener interfaces containing more than one event handling methods, jdk defines adapter classes. Examples are
-> For WindowListener a WindowAdapter
-> For MouseMotionListener a MouseMotionAdapter and many more...
-> Adapter classes provides definitions for all the methods (empty bodies) of their corresponding Listener interface
It means that MouseMotionAdapter class implements MouseMotionListener interface e.g.,
public interface MouseMotionListener {

    public void mouseDragged (MouseEvent me);

    public void mouseMoved (MouseEvent me);



public class MouseMotionAdapter implements MouseMotionListener {

    public void mouseDragged (MouseEvent me) { }

    public void mouseMoved (MouseEvent me) { }


Whenever class implements interface WindowListener or whatever a listener which contains methods more than one, we have to give the implementations of all containing methods, even if we don’t need them. But by extending our class with WindowAdapter or the with other Adapter classes we just give the implementation of our required one method. (For more information see “Interfaces” section)

public class EventHandler extends WindowAdapter {


-> Now our handler class will inherit from adapter class
-> Due to inheritance, all the methods of the adapter class will be available inside our handler class
-> Since adapter classes has already provided definitions with empty bodies.
-> We do not have to provide implementations of all the methods again
-> We only need to override our method of interest