Specifying the Public Interface of a Class
Behavior of bank
account (abstraction):
- deposit money
- withdraw money
- get balance
Methods
of
BankAccount
class:
- deposit
- withdraw
- getBalance
We want to support method calls such as the following:
harrysChecking.deposit(2000);
harrysChecking.withdraw(500);
System.out.println(harrysChecking.getBalance());
Method Definition
- access specifier
(such as
public)
- return type
(such as
String
or
void)
- method name
(such as
deposit)
- list of
parameters (double
amount
for
deposit)
- method body in
{ }
Examples:
public
void
deposit(double
amount)
{ . . . }
public
void
withdraw(double
amount)
{ . . . }
public
double
getBalance()
{ . . . }
Constructor Definition
A constructor initializes the instance variables
Constructor name = class name
Constructor body is executed when new object is created
Method Overload - you may have more than one constructor (same name, different number of parameters).
Example: public BankAccount(double initialBalance) { balance = initialBalance;}
Class Definition
