Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Hooks and Abstract Methods in Java

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.

like image 370
The Surrican Avatar asked Jan 25 '11 18:01

The Surrican


People also ask

What is difference between abstract class and abstract method in Java?

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).

What is difference between abstract and non abstract method in Java?

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.


2 Answers

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.

like image 109
jjnguy Avatar answered Oct 12 '22 18:10

jjnguy


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

  • The other methods in the class call the abstract methods.
  • Clients of the class must extend the class to provide the missing code.

2) Define a hook

  • The class has an associated callback interface (which is simply a bunch of abstract methods).
  • The methods of the class call the callback methods.
  • Clients of the class must implement the associated callback and register it to provide the missing code.

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.

like image 33
Bert F Avatar answered Oct 12 '22 19:10

Bert F