This is a quoted question from the study materials from my university.
It makes totally no sense to me.
For me hooks are specified points in (mostly sequential but not only) programs, where you can specify your own methods or callbacks to be executed.
For example an application has an "on before shutdown hook", i can register my callback method there that saves the user data to disk before shutdown.
Abstract methods are self explaining.
To me this is something completely different? or does either of those things have a 2nd meaning I don't know of? I did a quick search but didn't find anything.
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
Conclusion: The biggest difference between abstract and non abstract method is that abstract methods can either be hidden or overridden, but non abstract methods can only be hidden. And that abstract methods don't have an implementation, not even an empty pair of curly braces.
I really don't see these two things as being very similar. One way they may be related can be demonstrated in the following:
public abstract class AbstractActionDoer() {
public void doAction(Action act) {
beforeAction();
act.do();
afterAction();
}
protected abstract void beforeAction();
protected abstract void afterAtion();
}
public class DefaultActionDoer() extends AbstractActionDoer {
public void doAction(Action act) {
beforeAction();
act.do();
afterAction();
}
// default empty implementation
protected void beforeAction() { }
protected void afterAtion() { }
}
In this example, you have hooks that can be overridden in DefaultActionDoer
to change the functionality, but they are not required. This is similar to an abstract method because the abstract methods need to be overriden to define the functionality.
To me this is something completely different?
They are both methods to defer code to a client class (a user of your class):
1) Define an abstract method
2) Define a hook
There are of course a variety of reasons why you'd use one approach over the other. For example, a hook approach allows you to register multiple callbacks. But the abstract approach provides more direct access to the class (protected methods and ivars).
This is probably too much information, but in android programming you see both:
You can provide a CursorAdapter
to associate a data source to a UI widget.
http://developer.android.com/reference/android/widget/CursorAdapter.html
This class is abstract and has two methods newView
and bindView
that do the actual binding of the data to the UI widgets. Classes must subclass to use this class.
However, a subclass of CursorAdapter
is SimpleCursorAdapter
http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
This class implements newView
and bindView
and opts to provide a ViewBinder
interface that clients may implement and register with the SimpleCursorAdapter instance. The ViewBinder
instance must provide the setViewValue
method that binds a specific piece of data to a specific UI widget.
One key difference with SimpleCursorAdapter
is that providing a ViewBinder
is optional, which is one of the advantages of the hook approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With