SE450: Template Method: Interfaces and Abstract classes [31/35] Previous pageContentsNext page

Guideline: Always provide an interface. If you want to provide implementation for subtypes, you can additionally provide an abstract class:

package x;
public interface I {
  void m();
  void n();
}
public class IAbstract implements I {
  abstract public void m();  // m not implemented
  public void n() { ... }    // n implemented public
  protected void f() { ... } // f implemented protected
}

package y;
public class IObj extends x.IAbstract {
  public void m() { ...  f() ... }  // use IAbstract's implementation of f
}

package main;
public class Main {
  public static void main(String[] args) {
    x.I a = new y.IObj();
    a.m();
    a.n();
  }
}

Previous pageContentsNext page