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

Answer:

Use processDeposit() or processCheck().


Access Methods

class CheckingAccount
{
  private String accountNumber;
  private String accountHolder;
  private int    balance;
  . . . .
}

class CheckingAccountTester
{
  public static void main( String[] args )
  {
    CheckingAccount bobsAccount = new CheckingAccount( "999", "Bob", 100 );

    System.out.println( bobsAccount.getBalance() );
    bobsAccount.processDeposit( 200 );
    System.out.println( bobsAccount.getBalance() );

  }
}

A class with private data provides access to that data through its public methods. These methods are usually made public so other classes can use them. These methods provide an interface to the object.

The main() program above uses public methods of a CheckingAccount object to change the object's data. This is (usually) the correct way to use an object. The methods of an object know best how to change its instance variables. The idea of private is to enforce this correct use.


QUESTION 3:

Could an interface method check for errors and change instance variables only if the data is correct?


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