スーパークラスは、抽象メソッドを介してその義務の一部をサブクラスに委任できます。これの利点は、スーパークラスに一般的な再利用可能なロジックのみが含まれ、実装のより具体的な詳細が継承ツリーのサブクラスにプッシュされることです。
この状況では、スーパークラスは、いくつかのメソッドが既に実装されているかのようにアクセスする必要がある場合があります。これが、スーパークラスでボディのない抽象メソッドを定義する必要がある理由です。そうしないと、それらを参照できません!
たとえば、スーパークラスはデータの処理を処理できますが、具体的なサブクラスはさまざまなソースからのデータ入力を処理できます。この場合、スーパークラスとサブクラスは次のようになります。
class DataProcessor {
public void doWork() {
Data data = readData();
processData(data);
}
public abstract Data readData(); // this method is here and marked as abstract to make sure that:
// 1. child classes never fail to implement it;
// 2. parent class have a possibility to refer to the method that he is not actually implementing;
// 3. users of parent class have a possibility to refer to the method that parent is not capable of implementing.
}
class FileSystemDataProcessor extends DataProcessor {
public Data readData();
// handle data reading from the file system
}
}