Use processDeposit()
or processCheck()
.
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.
Could an interface method check for errors and change instance variables only if the data is correct?