Adding Abstraction

How to mark a class and its methods `abstract` so it can't be created on its own and every child class must fill in the missing methods.

Journeyman · Java · OOP · Updated

By

Often, when we refactor our code and create a base class, our code becomes so generalized that we no longer have enough information to create a fully usable object. This is an abstract class.

You can know that you are working with an abstract class when you need to specify the type of object you are working with.

For instance:

  • If you go to the bank to open an account, they will ask what type
  • If you need the area of a shape, you need to know what type
  • If you want to purchase an insurance policy, you have to specify what type of policy

In essence, any time you have to clarify which type of object you are working with, you are dealing with Abstract classes.

Abstract Classes

An abstract class is defined with the abstract keyword. By adding this keyword, you have specified that you cannot create an instance of this class.

public abstract class BankAccount
{
    protected int accountNumber;
    protected double balance;

    public BankAccount(int accountNumber, double balance)
    {
        this.accountNumber = accountNumber;
        this.balance = balance;
    }

    public int getAccountNumber() { return accountNumber; }

    public double getBalance() { return balance; }
}

I would never be able to create an instance of this class. The following code would fail because the BankAccount class is abstract.

BankAccount account = new BankAccount(123, 5000);

The only way to create a BankAccount object would be to create a new class that extends BankAccount. Since CheckingAccount is not marked as abstract, it is considered to be a concrete class.

public class CheckingAccount extends BankAccount
{
    private double overdraft = 100;

    public CheckingAccount(int accountNumber, double balance)
    {
        super(accountNumber, balance);
    }
}

We could create a new instance of CheckingAccount as follows:

CheckingAccount account = new CheckingAccount(123, 5000);

Abstract Methods

Abstract classes can also have abstract methods defined within them. An abstract method is a method signature that has no method body. Or in other words, it is a function without any logic. Why would Java allow this? Having a well defined method signature is the whole reason for class abstraction.

Remember that through inheritance, we have the ability to override methods of the parent class. With an abstract method, the parent class forces every child class to override the method. A class with an abstract method must itself be marked abstract, so you can't create an instance of it.

public abstract class BankAccount
{
    protected int accountNumber;
    protected double balance;

    public BankAccount(int accountNumber, double balance)
    {
        this.accountNumber = accountNumber;
        this.balance = balance;
    }

    public int getAccountNumber() { return accountNumber; }

    public double getBalance() { return balance; }

    public abstract double withdraw(double amount);

    public double deposit(double amount)
    {
        balance += amount;
        return balance;
    }
}

This also means that when my CheckingAccount class extends the BankAccount class, it MUST override the withdraw method.

public class CheckingAccount extends BankAccount
{
    private double overdraft = 100;

    public CheckingAccount(int accountNumber, double balance)
    {
        super(accountNumber, balance);
    }

    @Override
    public double withdraw(double amount)
    {
        if (balance - amount > -overdraft)
        {
            balance -= amount;
        }
        return balance;
    }
}

This guarantees that all BankAccount classes will have a withdraw method available to them, but it also ensures that each child of the BankAccount class must implement its own logic for how the withdraw method will work.

Do it: add a youth account

The bank adds a third kind of account for young savers: a youth account. It has two rules for withdrawals:

  • it never goes below zero, and
  • it never pays out more than $50 at one time.

BankAccount is given at the bottom of the file: the abstract class from above, with one more abstract method, kind(), which says what type of account it is. YouthAccount is started: it extends BankAccount and passes the account number and balance up with super(...). The starter doesn't compile yet: Java says YouthAccount doesn't override BankAccount's abstract methods. That error is your list of what to write.

withdraw(double amount) takes one value:

  • amount: how much the account holder wants to take out, a double, such as 30.

Like CheckingAccount's, it returns the balance after the withdrawal. When the withdrawal isn't allowed, it changes nothing and returns the balance as it was.

For example, youth account 3001 with $80 in it:

Withdraw Over $50? Below zero? Returns
30 No No: 80 − 30 = 50 50.0
60 Yes No: 80 − 60 = 20 80.0: not allowed
90 Yes Yes: 80 − 90 = −10 80.0: not allowed

So new YouthAccount(3001, 80).withdraw(30) returns 50.0, and getBalance() is then 50.0.

Your job: finish YouthAccount by overriding BankAccount's two abstract methods.

  • kind() returns "Youth".
  • withdraw(double amount) changes nothing when amount is more than 50. Exactly 50 is allowed.
  • It changes nothing when taking amount out would leave the balance below zero. Leaving exactly 0 is allowed.
  • Otherwise, it takes amount off the balance.
  • Either way, it returns the balance.
  • Leave BankAccount as it is.

Practiced live on the lesson page: write the Java right there and check it.

Cheat sheets