Introduction
Java is an object-oriented programming language. In the previous lessons we have already become acquainted with the term class. Its meaning and usefulness may not be very clear yet. In the lesson Classes and Objects we explain both terms and illustrate the power of this concept to develop re-usable code.
Problem Description
A bank manages multiple types of accounts: they offer checking accounts, saving accounts and individual retirement accounts to their customers. For all these accounts the IT division has to create programs to deposit money, to withdraw money and to query the account balance. For this simple example a programmer has to develop three separate programs each with three methods (depositMoney(), withdrawMoney(), getBalance()). It is easy to understand that these methods are identical for all three programs. Only the data item accountBalance will differ for the three accounts. A class with its corresponding objects will make it possible to re-use some code.
Terminology
What is a Class?
A class is a collection of methods and data used as a template (a blueprint) for creating objects. The class methods and data are called members (member methods, member data) of the class. Referring to our example, the variable accountBalance is an example of member data and depositMoney(), withdrawMoney() and getBalance() are member methods of the class. We need these member methods and the corresponding member data item for all types of accounts. We can give our class (template) the name Account. It is good practice to start the name of a class with a capital letter.
The class definition for our example will have the following structure:
public class Account{ private double accountBalance; public ... depositMoney(...){ ... } public ... withdrawMoney(...){ ... } public ... getBalance(...){ ... } }
What is an Object?
An object is an instance of a class. An object will store and manipulate real data. Each object will contain the member data and the member methods defined in the class. For our problem, a programmer will for example create the objects checkingAccount, savingsAccount and retirementAccount based on the class Account. The three objects will use the same member methods (depositMoney(), withdrawMoney() and getBalance()). All objects will have a member variable called accountBalance. The value of accountBalance varies between the three objects. One object (e.g. checkingAccount) cannot access the value of accountBalance of another object (e.g. savingsAccount).
The three objects can be defined as follows:
Account checkingAccount = new Account(); Account savingsAccount = new Account(); Account retirementAccount = new Account();
The animation shows the creation of three objects based on the class Account:

An Object in Memory
The statement Account checkingAccount = new Account(); consists of two main parts:
Account checkingAccount; checkingAccount = new Account();
- Account checkingAccount: creates an empty reference variable checkingAccount that will be used to point to an object of the class Account.
- checkingAccount = new Account(): the new operator creates an object of the class Account. The new operator also returns a reference to the memory space reserved for this object. The reference variable checkingAccount receives this reference value (assignment operator =) and points to the memory space reserved for this object.
The following animation is a simulation how the object checkingAccount is created and referenced in memory:

Develop the Banking Application
The Account Class
The accessibility of a class must be declared private or public. If only one class is defined in a file the accessibility of the class must always be public. Only in some special cases can the accessibility of a class be private.
public class Account{ ... }
The accessibility of a class is almost always public.
Member Data
We use the member variable accountBalance to store the balance of an account. The data type of this variable will be double.
For all member data we have to decide wether a variable is publicly accessible (public) or privately accessible (private). Public member data is directly accessible from an object created from another class . A private data member can only be accessed by methods from the same class. To protect member data it is recommended to keep these member variables always private.
private double accountBalance;
Always try to declare the accessibility of a member variable as private.
Member Methods
Three methods need to be developed: depositMoney(), withdrawMoney() and getBalance() .
depositMoney()
depositMoney() is the method to be used to increase the value of the member variable accountBalance. The method receives the value of the new deposit (double amount) and increases the value of accountBalance. The method does’t return any value (void). The method must be declared public since the method will be invoked from outside the class.
public void depositMoney(double amount){ accountBalance = accountBalance + amount; }
withdrawMoney()
withdrawMoney() is the method to be used to decrease the value of the member variable accountBalance. The method receives the value of the amount to be withdrawn (double amount) and decreases the value of accountBalance. The method does’t return any value (void). The method must be declared public since the method will be accessed from outside the class.
public void withdrawMoney(double amount){ accountBalance = accountBalance - amount; }
getBalance()
Member data can be initialised through the constructor. A constructor is a special method that automatically will be called when an object is created. The method is special since a constructor never returns a value. A constructor gets the same name as the class to which it belongs (e.g. Account).
getBalance() is a method to inform the user about the balance of an account. This method returns the value of the member data accountBalance (data type double). The member method must be publicly accessible (public) because we will invoke this method from another class.
public double getBalance(){ return(accountBalance); }
Constructor
Member data can be initialised through a constructor. A constructor is a special method that automatically will be called when an object is created. The method is special since a constructor never returns a value. A constructor gets the same name as the class to which it belongs (e.g. Account).
public Account(double initialDeposit){ accountBalance = accountBalance + initialDeposit; }
A constructor never returns a value!
A default constructor without arguments (e.g. Account()) will be automatically generated when no constructor method is defined in the class.
How to Use the Account Class?
Create an Account Object
An Account object can be created using one of the following statements:
Without constructor (constructor automatically generated) Account checkingAccount = new Account(); With constructor (public Account(double initialDeposit);) Account checkingAccount = new Account(1000.00);
Account Methods
A method in an object can be called by the following statement:
Method without return statement referenceVariable.methodName(arguments); Method with return statement variable = referenceVariable.methodName(arguments);
checkingAccount.depositMoney(2000.00); checkingAccount.withdrawMoney(1000.00); balanceTotal = checkingAccount.getBalance();
Java Program
The Account Class
The Account class is stored in a file named Account.java:
//Account Class //Carlos De Backer public class Account{ private double accountBalance; public Account(){ accountBalance = 0.0; } public Account(double initialDeposit){ accountBalance = 0.0; accountBalance = accountBalance + initialDeposit; } public void depositMoney(double amount){ accountBalance = accountBalance + amount; } public void withdrawMoney(double amount){ accountBalance = accountBalance - amount; } public double getBalance(){ return(accountBalance); } }
Two methods have the same method name: the constructor methods Account() and Account(double initialDeposit) . This is not a problem in Java. Java allows to define multiple methods with the same name. This is called method overloading. The methods should differ only in the number or data type of parameters. The number and/or type of the arguments provided in the calling method, will determine which method will be chosen.
Multiple methods can have the same name (method overloading). Methods with the same name should differ in the number or data type of parameters.
The AccountExample Class
The AccountExample class is stored in a file named AccountExample.java and contains the main() method:
//AccountExample Class //Carlos De Backer public class AccountExample{ public static void main(String[] args){ Account checkingAccount = new Account(1000.00); //initialDeposit will be 1000.00 Account savingsAccount = new Account(); Account retirementAccount = new Account(); checkingAccount.depositMoney(5000.00); System.out.println("Deposit - Checking Account: "+checkingAccount.getBalance()); checkingAccount.withdrawMoney(2000.00); System.out.println("Withdraw - Checking Account: "+checkingAccount.getBalance()); savingsAccount.depositMoney(2000.00); System.out.println("Deposit - Savings Account: "+savingsAccount.getBalance()); retirementAccount.depositMoney(500.00); System.out.println("Deposit - Retirement Account: "+ retirementAccount.getBalance()); } }
Deposit - Checking Account: 6000.0 Withdraw - Checking Account: 4000.0 Deposit - Savings Account: 2000.0 Deposit - Retirement Account: 500.0
The statement Account checkingAccount = new Account(1000.00) will select the constructor method Account(double initialDeposit). The statement Account savingsAccount = new Account() will select the constructor method Account().