Skip to main content
Lesson 6 - Defining and Using Classes
ZIPPDF (letter)
Lesson MenuPreviousNext
  
Determining Object Behavior page 4 of 11

  1. In this section, you will learn how to create a simple class that describes the behavior of a bank account. Before you start programming, you need to understand how the objects of your class behave. Operations that can be carried out with a checking account could consist of:

    • Accept a deposit
    • Withdraw from the account
    • Get the current balance

  2. In Java, these operations are expressed as method calls. For example, assume we have an object checking of type CheckingAccount. The methods that invoke the required behaviors

    checking.deposit(1000)
    checking.withdraw(250)
    System.out.println("Balance: " + checking.getBalance());

    are represented by the set of methods

    - deposit
    - withdraw
    - getBalance

    These methods form the behavior of the CheckingAccount class. The behavior is the complete list of the methods that you can apply to objects of a given class. An object of type CheckingAccount can be viewed as a "black box" that can carry out its methods.

  3. To construct objects of the CheckingAccount class, it is necessary to declare an object variable

    CheckingAccount checking;

    Object variables such as checking are references to objects. Instead of holding an object itself, a reference variable holds the information necessary to find the object in memory.

  4. This object variable checking does not refer to any object at all. An attempt to invoke a method on this variable would cause the compiler to generate an error indicating that the variable had not been initialized. To initialize the variable, it is necessary to create a new CheckingAccount object using the new operator

    checking = new CheckingAccount();

    This call creates a new object and returns a reference to the newly created object. To use an object, you must assign that reference to an object variable.

  5. We will implement (that is, create and write the code for) the CheckingAccount object so that the account has an initial balance of 1000.0 dollars.

    // open a new account
    double initialDeposit = 1000.0;
    CheckingAccount checking = new CheckingAccount();
    
    // set initial balance to 1000.0
    checking.deposit(intialDeposit);

    Figure 6-1. Creating a New Object

  6. Objects of the CheckingAccount class can be used to carry out meaningful tasks without knowing how the CheckingAccount objects store their data or how the CheckingAccount methods do their work. This is an important aspect of object-oriented programming.

  7. Once we understand how to use objects of the CheckingAccount class, it is possible to design a Java class that implements its behaviors. To describe object behavior, you first need to implement a class, and then implement methods within that class.

    public class CheckingAccount
    {
      // CheckingAccount data
    
      // CheckingAccount constructors
    
      // CheckingAccount methods
    }

    Next we implement the three methods that have already been identified:

    - deposit
    - withdraw
    - getBalance
    public class CheckingAccount
    {
      // CheckingAccount data
    
      // CheckingAccount constructors
    
      public void deposit( double amount )
      {
        // method implementation
      }
     
      public void withdraw( double amount )
      {
        // method implementation
      }
      
      public double getBalance()
      {
        // method implementation
      }
    }
  8. A method header consists of the following parts:

    access_specifier return_type method_name ( parameters )
    1. An access_specifier (such as public). The access specifier controls which other methods can call this method. Most methods should be declared as public so all other methods in your program can call them.

    2. The return_type of the method (such as double or void). The return type is the type of the value that the method computes. For example, in the CheckingAccount class, the getBalance method returns the current account balance, which is a floating-point number, so its return type is double. The deposit and withdraw methods don't return any value. To indicate that a method does not return a value, you use the special type void.

    3. The method_name (such as deposit).

    4. A list of the parameters of the method. The parameters are the input to the method. The deposit and withdraw methods each have one parameter, the amount of money to deposit or withdraw. The type of parameter, such as double, and name for each parameter, such as amount, must be specified. If a method has no parameters, like getBalance, it is still necessary to supply a pair of parentheses () behind the method name.

  9. Once the method header has been specified, the implementation of the method must be supplied in a block that is delimited by braces {...}. The CheckingAccount methods will be implemented later in Section D.


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.