go to previous page   go to home page   go to next page

Do you want to keep hackers out of your checking account?

Answer:

Yes.


The private Visibility Modifier

When an instance variable is declared private it can be used only by the methods of that class. Here is the checking account class definition from the last chapter. Each of its variables declared to be private.

Only the methods of a CheckingAccount object can "see" accountNumber, accountHolder, and balance.


public class CheckingAccount
{
  // variable declarations
  private String accountNumber;
  private String accountHolder;
  private int    balance;

  //constructor
  public CheckingAccount( String accNumber, String holder, int start )
  {
    accountNumber = accNumber ;
    accountHolder = holder ;
    balance       = start ;
  }

  // methods
  public int getBalance()
  {
    return balance ;
  }

  public void  processDeposit( int amount )
  {
    balance = balance + amount ; 
  }

  public void processCheck( int amount )
  {
    int charge;
    if ( balance < 100000 )
      charge = 15; 
    else
      charge = 0;

    balance =  balance - amount - charge  ;
  }
  
  public String toString()
  {
     return "Account: " + accountNumber + ";\tOwner: " + accountHolder + ";\tBalance: " + balance ;
  }
}

QUESTION 2:

How can you change the balance in a CheckingAccount object?


go to previous page   go to home page   go to next page