20-CS-694 Advanced Programming Techniques Spring 2012

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

All lectures
Some Intuition About Java Class Hierarchies

This sequence is intended to aid in understanding a few things about Java:

  1. A component such as JButton can be used inside another component such as JFrame regardless of whether JButton subclasses JFrame or vice versa or neither subclasses the other
  2. When a class implements an interface, say I, objects of that class also are of type I. This allows a class such as JButton, which provides a service (doing something when a button is pressed), to listen to an object of any class (which is also of type I) and to provide a callback to that object.
  3. In Java we can inherit from at most one super class but we can achieve the same power as in multiple inheritance by creating an object of the class that will provide the needed service and run methods of that class from that object.

    1.   A1.java

    A MouseListener is implemented. Then A11 objects also are of type MouseListener. Hence butt.addMouseListener(this) establishes a link for the superclass of JButton to communicate back to A11 objects via actionPerformed(). Only objects of type MouseListener are allowed to do this but any data type can also be made an object of type MouseListener by implementing the MouseListener interface. Because the MouseListener interface requires implementing mouseExited(), mouseEntered(), and mouseClicked(), these methods have to appear here as doing nothing.
    2.   A2.java

    Alternatively, a special instance of the class MouseAdapter can be created to provide the service of listening to mouse events. Then objects of type A21 are not of type MouseListener, but that is OK since the special MouseAdapter object handles the listening. This means butt.addMouseListener(this) cannot be used because this does not refer to a MouseListener object. But butt.addMouseListener(ma) can be used because ma is of type MouseAdapter, which implements MouseListener and has the needed type. Notice that the callback never gets back to the A21 object w - instead it is directed to the ma object. The object w never needs to access the ma object because the ma object takes care of all the mouse event servicing that needs to be done.
    3.   A3.java

    In the previous code services from both MouseAdapter and JFrame were used. Since only JFrame was extended, an object of class MouseAdapter was created and passed to the listener of mouse events via butt.addMouseListener(ma). But the roles can be flipped: that is, the MouseAdapter can be extended and a JFrame object created. Then butt.addMouseListener(this) is used because A31 objects are also MouseListener objects via extending from MouseAdapter. Also j.add("North",butt) is used instead of add("North",butt) because A31 objects are not JFrame objects - instead they use the services of a JFrame through an object j created in the constructor of A31. In addition, w.setSize() nor w.setVisible() can be used because w is not of type JFrame. These are replaced by j.setSize() and j.setVisible() in the constructor of A31. Finally, mousePressed() and mouseReleased() override methods of the same name in the MouseAdapter class (which A31 subclasses).
    3.   A4.java

    There is nothing magic about the MouseAdapter class. We can create such a class ourselves. All that is needed is to implement the MouseListener interface and supply all the required methods.