# Regarding programming languages, what is meant by “hook”?

My professor gave this assignment question, where he asks us to compare the expressive ease and the expressive range of Low Level and High Level theoretical programming languages.

While in context, I could understand that "hook" seems to refer to the opposite of an abstraction, I don't understand precisely what it means. Is it a real term, or did my professor (who isn't being helpful) make it up?

This is the question, cut from the assignment:

-
I've never heard of a hook being used to describe the opposite of an abstraction before. I suspect it's a term your professor has made up. – Matt Lewis Jan 24 '13 at 22:53
I have no idea about this question, and cannot actually see how it relates to programming languages. I agree with @MattLewis that the professor is probably making these notions up. – Dave Clarke Jan 25 '13 at 6:59
What's this, computer science as invented in a sociology department? Also, have you asked your professor? – Andrej Bauer Jan 25 '13 at 12:09

Typically, a hook is an non-abstract method defined in an abstract class, that can be optionally implemented and overridden in subclasses to modify behaviour. I think this is best exemplified in the template method pattern, where the (abstract) superclass defines the overall semantics for a process, but leaves primitive operations to be implemented by sublasses:

A template method is defined inside an abstract class:

public abstract class AbstractLogger {

/*
* Template method, made final to make
* sure the flow is not modified by subclasses
*/
public final void logMessage(String msg) {
msg = preProcess(msg);
writeToLog(msg);
}

/*
* Abstract primitive operation that must be
* implemented by subclasses
*/
protected abstract void writeToLog(String str);

/*
* Hook method that can be optionally overriden to
* modify the template method's behaviour
*/
protected String preProcess(String str) {
return str; // default behaviour: do nothing
}

}


Concrete subclasses make use of the hook-method to augment behaviour:

public abstract class TerminalLogger extends AbstractLogger {

/*
* This primitive operation must be implemented
*/
protected void writeToLog(String str) {
System.out.println(str);
}

/*
* Override super.preProcess() to modify behaviour
* of the template method
*/
protected String preProcess(String str) {
return str.toLowerCase();
}
}


Pardon the contrived example - I hope it illustrates the point clearly enough. For more info on hook methods and template methods see:

http://en.wikipedia.org/wiki/Hook_method#Hook_methods

http://en.wikipedia.org/wiki/Template_method_pattern

The template method pattern wiki presents a fuller example than mine - you might want to check it out.

-
THanks for the explanation of the concept, however it doesn't entirely jive with the context my professor used it in – CodyBugstein Jan 27 '13 at 17:22