As I understand it, a hook method is a method that does nothing other than returning to its caller. For example, this is a hook method:
void doNothing( String unusedArg){ return; }
Hook methods are typically defined in what are called âadaptor classesâ which are classes that implement interfaces by overriding a number of abstract methods inherited from an interface with hook methods. The use of such an adaptor class is for when you want a subclass to implement an interface but you do not intend to make your program use all the methods in the interface - you intend to use only some of these methods, and so you do not want to clatter up the code in the subclass with overriding methods which you are sure will never be used. In this situation, rather than make a class implement an interface directly, you can make the class extend the corresponding adaptor class (which implements that interface with hook methods) to avoid such clatter. An example of such an adaptor class is the MouseAdaptor class which is in the package java.awt.event and has code (excluding comments):
public abstract class MouseAdapter implements MouseListener {
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
All the hook methods above overrides the abstract methods inherited from MouseListener interface.
Andrew:)
|