Skip to main content
Lesson 6 - Defining and Using Classes
ZIPPDF (letter)
Lesson MenuPreviousNext
  
Implementing Methods page 6 of 11

  1. An implementation must be provided for every method of the class. The implementation for three methods of the CheckingAccount class is given below.

    public class CheckingAccount
    {
      private double myBalance;
      private String myAccountNumber;
    
      public double getBalance()
      {
        return myBalance;
      }
    
      public void deposit( double amount )
      {
        myBalance =  myBalance + amount;
      }
    
      public void withdraw( double amount )
      {
        myBalance =  myBalance - amount;
      }
    }
  2. The implementation of the methods is straightforward. When some amount of money is deposited or withdrawn, the balance increases or decreases by that amount.

  3. The getBalance method simply returns the current balance. A return statement obtains the value of a variable and exits the method immediately. The return value becomes the value of the method call expression. The syntax of a return statement is:

    return expression;

    or

    return;  // Exits the method without giving back a value

Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.