18. Inheritance
Inheritance
Remember the discussion we had on the Automobile superclass and some of its possible subclasses?
Let's take a look at building and subclassing a Java class.
Class BankAccount
Let's build a Java class BankAccount, which we can then subclass by way of illustration of the principles of inheritance. Inheritance is central to OOP, and no text on OOP would be complete without a treatment of this important concept. Inheritance is embodied by the following:
A subclass inherits the public variables and methods of the superclass, and has access to the public constructors of the superclass.
It is important to understand that inheritance is founded on the public variables and methods of the superclass; a subclass has no direct access to the private variables and methods of the superclass. The superclass has no access to the variables and methods of its subclass.
Consider a simple BankAccount class, which embodies:
- A private variable
balance, the amount of money in theBankAccount - Constructors for:
- A
BankAccountwith a zero initial balance - A
BankAccountwith an initial balance other than zero - Methods for:
- depositing money in the
BankAccount - withdrawing money from the
BankAccount - returning the current balance in the
BankAccount
This is a fairly simple class, which is self-explanatory. Note the two constructors.
Subclassing class BankAccount
When subclassing another class, we rely on inheriting the public methods and variables of the class, and add only those variables and methods to the subclass that provide the extra or specialised functionality of the subclass. BankAccount contains the functionality that is common to most kinds of bank account, so to produce a sensible and useful subclass, we need a situation where we recognise a type of bank account that requires some specialisation that BankAccount doesn't provide us with. A savings account is usually an interest-bearing account, but BankAccount doesn't have (and doesn't need) a means to calculate interest on the balance in the account.
To deal with this, we write a subclass of the BankAccount class, called SavingsAccount which contains the extra functionality. Being a subclass of BankAccount, SavingsAccount will have methods:
- void deposit(double amount)
- void withdraw(double amount)
- double getBalance()
These are the public methods of BankAccount, and they are inherited by SavingsAccount. Additionally, SavingsAccount must include a variable that holds the interest rate applicable to the SavingsAccount, must have a method which calculates the interest and adds it to the balance in the account, and another which retrieves the interest rate. To write a subclass, we must include the keyword extends and the name of the class we are extending in the class definition:
public class SavingsAccount extends BankAccount {
...
}
Here is the source code for class SavingsAccount:
Note that although balance is a private variable of class BankAccount, class SavingsAccount has no private variable of its own called balance. Where will the SavingsAccount store its current balance? In the private BankAccount variable balance - this is possible because BankAccount has a public method called deposit() which adds money to the private balance variable of the BankAccount object. Even though a subclass has no direct access to the private variables of its superclass, if the superclass has a public method that accesses the private variable, we can use this public method to access the superclass private variable. This is as it should be - private variables can only be accessed by the methods of their class, and if we use the inherited superclass method deposit(), we are adding money to the superclass balance. We can see the balance in the SavingsAccount by using the BankAccount public method getBalance().
To use the inherited methods of the superclass, we employ the keyword this, which refers to the current instance of the class, i.e. the current instance of SavingsAccount. For example, in the constructor for SavingsAccount:
public SavingsAccount(double rate, double initialBalance) {
interestRate = rate;
this.deposit(initialBalance);
}
© G. Hearn, & University of the Western Cape, 2006






