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

user517491 picture user517491 · Oct 20, 2011 · Viewed 10.1k times · Source

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.

Answer

Joachim Sauer picture Joachim Sauer · Oct 20, 2011

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.