Difference between Hooks and Abstract Methods in Java
Asked Answered
W

7

14

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.

Wolfhound answered 25/1, 2011 at 18:36 Comment(2)
Please do not tag PHP to the questions not related to PHP. (Removed it).Assoil
Maybe you could post the definition of each as written in your study guide ?Fulmis
D
3

Personally, I would go to the professor and ask for clarification before asking the StackOverflow community.

...but, that being said, I believe (if your description of a hook is correct) that any class can register a callback method for a hook whereas an abstract method forces a child class a to implement its own algorithm for the given method.

Dilorenzo answered 25/1, 2011 at 18:39 Comment(6)
Why assume that he didn't already ask the prof? And considering the number of homework problems that get asked (and answered on SO), it perfectly reasonable to ask it hereLaveta
@Laveta - I assume the OP didn't ask the professor because the OP tells us his interpretation of what a hook is...not what the professor defined a hook as. And I never said the question didn't belong here...I merely said he should try to get his information from the source rather than second hand first.Dilorenzo
@Justin -- So why assume that he didn't try other resources? Why assume that some other SO user is lazy? How about give him the benefit of the doubt absence evidence to the contrary. Is Joe supposed to list out the names and addresses of all the other students and professors to indicate that he had met the arbitrarily Justin threshold before asking a question? Really. Lets assume a prof has office hours that mean Joe can't talk to the prof for 3 days (over a weekend). Does this mean Joe is not allowed to ask the SO community?Laveta
not so easy to ask the prof. not such a good academic system here... however it is a programming related question so i thought give it a shot.Wolfhound
@Juan - Everybody seems to be missing the second part of the answer. It may not be a long drawn out description, but it does answer the question. Short and sweet.Dilorenzo
Snarks don't add value. Don't complain that people are pointing that out. Remove the snark.Laveta
G
15

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.

Gluck answered 25/1, 2011 at 18:44 Comment(1)
Hooks methods customise generic framework components to run application-specific logic. Example of a hook method is the run() method inside a thread in java. It's up to you to add application-specific logic inside this method.Ninfaningal
H
5

Abstract methods are one way of implementing "hooks". Hooks can be implemented through callbacks, observer pattern, plugins... Any way you'd like to specify that some code from the outside gets to run at important points, called hooks, in your app.

Housemaster answered 25/1, 2011 at 19:1 Comment(0)
C
5

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.

Companionway answered 25/1, 2011 at 19:8 Comment(0)
D
3

Personally, I would go to the professor and ask for clarification before asking the StackOverflow community.

...but, that being said, I believe (if your description of a hook is correct) that any class can register a callback method for a hook whereas an abstract method forces a child class a to implement its own algorithm for the given method.

Dilorenzo answered 25/1, 2011 at 18:39 Comment(6)
Why assume that he didn't already ask the prof? And considering the number of homework problems that get asked (and answered on SO), it perfectly reasonable to ask it hereLaveta
@Laveta - I assume the OP didn't ask the professor because the OP tells us his interpretation of what a hook is...not what the professor defined a hook as. And I never said the question didn't belong here...I merely said he should try to get his information from the source rather than second hand first.Dilorenzo
@Justin -- So why assume that he didn't try other resources? Why assume that some other SO user is lazy? How about give him the benefit of the doubt absence evidence to the contrary. Is Joe supposed to list out the names and addresses of all the other students and professors to indicate that he had met the arbitrarily Justin threshold before asking a question? Really. Lets assume a prof has office hours that mean Joe can't talk to the prof for 3 days (over a weekend). Does this mean Joe is not allowed to ask the SO community?Laveta
not so easy to ask the prof. not such a good academic system here... however it is a programming related question so i thought give it a shot.Wolfhound
@Juan - Everybody seems to be missing the second part of the answer. It may not be a long drawn out description, but it does answer the question. Short and sweet.Dilorenzo
Snarks don't add value. Don't complain that people are pointing that out. Remove the snark.Laveta
L
3

There was definitely some conflation of concepts with implementation details.

Basically your understanding is correct.

To quote back the OP's statement, the what is :

Hooks are specified points, where the developer can specify methods or callbacks to be executed.

Javascript language is probably the most (notorious) language for allowing everything to be hooked. (As a side note: Google had a security hole that involved the Javascript Array constructor being hooked)

Implementing an abstract method/callback interface is the mechanism how the callback is realized in java code.

Laveta answered 25/1, 2011 at 18:57 Comment(0)
I
1

A hook is an old term, probably dating to before the 1980s. It's not clear from your question if "in Java" applies to both Hooks and Abstract Methods, but if I were a professor (!), I would think it's important to understand the history of hooks (before Java).

There's a Wikipedia page that goes into great detail about Hooking. Personally, when I think of hook, I think of mechanisms that allow run-time customization.

Here are a few definitions found at http://www.tldp.org/LDP/Linux-Dictionary/html/h.html

Hook

A feature included in a software or hardware product to enable hobbyists and programmers to add their own custom features. From QUECID

hook

n. A software or hardware feature included in order to simplify later additions or changes by a user. For example, a simple program that prints numbers might always print them in base 10, but a more flexible version would let a variable determine what base to use; setting the variable to 5 would make the program print numbers in base 5. The variable is a simple hook. An even more flexible program might examine the variable and treat a value of 16 or less as the base to use, but treat any other number as the address of a user-supplied routine for printing a number. This is a hairy but powerful hook; one can then write a routine to print numbers as Roman numerals, say, or as Hebrew characters, and plug it into the program through the hook. Often the difference between a good program and a superb one is that the latter has useful hooks in judiciously chosen places. Both may do the original job about equally well, but the one with the hooks is much more flexible for future expansion of capabilities (EMACS, for example, is all hooks). The term `user exit' is synonymous but much more formal and less hackish. From Jargon Dictionary

hook

The technique of inserting code into a system call in order to alter it. The typical hook works by replacing the function pointer to the call with its own, then once it is done doing its processing, it will then call the original function pointer. From Hacking-Lexicon


As for Abstract methods in Java, it was pretty well explained in Bert F's answer. I'll stress that unlike the general notion of hooks (which allows customization), abstract methods require specification, meaning there is no default definition.

Illdisposed answered 13/2, 2017 at 15:18 Comment(0)
I
0

To my understanding:

Abstract Methods

  • required
  • steps that must be customized/implemented

Hooks

  • optional
  • abstract class may provide a default implementation

Example

public abstract class AbstractClass() {
    public void final templateMethod() {
        operation1();
        operation2();
        operation3();
        operation4();
    }
    void final operation1() {/* do some stuff, cannot be changed */}
    void final operation2() {/* do some other stuff which cannot be overridden */}
    abstract void operation3(); // ConcreteClass has to implement it
    void operation4() {/* do nothing */} // This is effectively a hook
}

public class ConcreteClass() extends AbstractClass {
    @Override
    void operation3() {/* implementing mandatory step*/}

    @Override //optional, I don't have to implement/override this method
    void operation4() {/* I can choose if more detailed steps needs to be implemented */}
}

Intended answered 18/1, 2024 at 22:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.