Designing basic Java classes

Your task

You can find today’s task with its short description here.

Extra notes

Task contains some usage of abstract classes. We’ve shown how to use them to create common fields only in abstract base class and reuse this code in subclasses.

To do that we had to contain some actual fields in BankAction

public abstract class BankAction {

  protected final double amount;
  protected final Timestamp begin;
  protected final Timestamp end;

  protected BankAction(double amount, Timestamp begin, Timestamp end) {
    this.amount = amount;
    this.begin = begin;
    this.end = end;
  }

  /* ... */
}

and then call defined constructor in subclasses

public class LoanBankAction extends BankAction {

  private final String trustee;

  public LoanBankAction(double amount, Timestamp begin, Timestamp end, String trustee) {
    super(amount, begin, end);
    this.trustee = trustee;
  }

  /* ... */
}

Additionally, we’ve seen on our exercises classes that we can call other constructors from our constructor using this keyword inside the constructor body - the only limitation here is that it has to be the first instruction inside the constructor body.

public class LoanBankAction extends BankAction {

  /* ... */

  public LoanBankAction(LoanBankAction loanBankAction) {
    this(amount, begin, end, trustee);
  }

  /* ... */
}
Previous
Next