'Progressive abstract class extension in Java pattern

Suppose I have an abstract class that defines an endpoint as such, and a class that overrides said endpoint and whose purpose is to wrap this endpoint in a try catch for example.

public abstract class Endpoint {
  public abstract void handle(Request req, Response res);
}
public abstract class ErrableEndpoint {
  public void handle(Request req, Response res){
    try {
       handleRequest(req, res);
    } catch(Exception e){
      // handle the error in some way
    }
  }

  public abstract void handleRequest(Request req, Response res);
}
public class UserEndpoint extends ErrableEndpoint {
  public void handleRequest(Request req, Response res){ 
    // Do things which might throw an error.
  } 
}

Now, if I extend this class further, using still abstract classes to allow for a creation of a hierarchy, I end up renaming methods again, and again, and again.

There are currently 2 things I've considered.

  1. Using interfaces - This is currently outside of the realm of possibility due to the fact that I have state within the endpoint which is passed through a constructor. I'm also not looking for lambda capability, so it's not an issue.

  2. Calling .super() - I'm looking to avoid this, as I'm working on a framework-oriented project, where I would like the people who use it to not have to call super, and where they should be able to mindlessly extend. Requiring super by convention could lead to weird buggy behavior if the user forgets to call it. This is avoided with the method renaming pattern I'm currently using.

Essentially, I'm looking for a way to continuously add functionality on top of the same method, without having to constantly rename it on each extension.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source