Alex Rivera | Logout

Is there a way to make a method which is not abstract but must be overridden?

Asked 2011-10-20T07:21:34.167
58

Is there any way of forcing child classes to override a non-abstract method of super class?

I need to be able to create instances of parent class, but if a class extends this class, it must give its own definition of some methods.

Edit
Report

1 Answer

37

There is no direct compiler-enforced way to do this, as far as I know.

You could work around it by not making the parent class instantiable, but instead providing a factory method that creates an instance of some (possible private) subclass that has the default implementation:

public abstract class Base {
  public static Base create() {
    return new DefaultBase();
  }

  public abstract void frobnicate();

  static class DefaultBase extends Base {
    public void frobnicate() {
      // default frobnication implementation
    }
  }
}

You can't write new Base() now, but you can do Base.create() to get the default implementation.

answered 2011-10-20T07:25:16.957

Your Answer