Java Reference
In-Depth Information
When defining a class, you place all constructor and method definitions inside, like
this:
public class BankAccount
{
// Constructors
public BankAccount()
{
bodyÏfilled in later
}
public BankAccount(double initialBalance)
{
bodyÏfilled in later
}
// Methods
public void deposit(double amount)
{
bodyÏfilled in later
}
public void withdraw(double amount)
{
bodyÏfilled in later
}
public double getBalance()
{
bodyÏfilled in later
}
private fieldsÏfilled in later
}
You will see how to supply the missing pieces in the following sections.
The public constructors and methods of a class form the public interface of the class.
These are the operations that any programmer can use to create and manipulate
BankAccount objects. Our BankAccount class is simple, but it allows
programmers to carry out all of the important operations that commonly occur with
bank accounts. For example, consider this program segment, authored by a
programmer who uses the BankAccount class. These statements transfer an amount
of money from one bank account to another:
// Transfer from one account to another
double transferAmount = 500;
momsSavings.withdraw(transferAmount);
Search WWH ::




Custom Search