Java #15 – Creating a Class

Java is an object-oriented language. Object-oriented means that you
program by thinking about and describing the things, or objects that your program will
model—that is, the data, instead of thinking in a process-oriented
way. Java programs consist of a collection
of classes, or blueprints, from which we create
objects. The class describes the characteristics
(called attributes) and behaviors (called methods) of the
object. Generally, attributes correspond to nouns
object. or adjectives that give more
information about the object (location, size, color, amount, et
cetera). Attributes are like variables associated
with the object. Methods correspond to verbs—things
the object knows how to do. Examples might be moving, or
setting the color, or creating a String describing the
object. Just having a blueprint doesn’t give
you a building. You have to create the building. And, you can make multiple buildings
from the same blueprint. It’s the same with objects. You don’t have an object until you
make a new one. Here we see that we are creating two
objects p1 and p2 which are of the class
Point. The class Point has attributes x and
y, the location of the Point. Note that p1 and p2 will have different
values for these. In the same way, if you built two
buildings from the same blueprint, you might paint one blue and the
other brown. To illustrate, we’ll create a small
class for Bank Accounts. First, we think about the attributes
and methods we might want to have, then we’ll write some code. For simplicity, we’ll say a bank
account only has an owner and a balance. (You might think about account types,
or account numbers, or all sorts of other things, but we’ll
keep it simple). The account will allow you to either
deposit money, withdraw money, or check the balance. We start by declaring the attributes
belonging to this class, owner and balance. This is like declaring variables in a
method, but note the addition of “private” at
the front. This means that these will not be
visible outside the class. Instead, other classes must use
methods to access or change these. At first, it seems like this makes you
write more code and is less convenient. For very small programs, that’s
probably true, but it does make it easier to change
the code later in larger programs. Suppose our bank creates a new
program where they send you a text message whenever
your balance gets low. If we made balance a public attribute,
we might have to search many different files to look
for balance changes. By making it private, we know that all
changes occur in this one file and we can simply add code
here for the new feature. Below the attributes we have a
constructor method. Remember we don’t actually have any
objects until we call a constructor. Constructor methods don’t have a
return type, and are named the same as the class. This one simply copies the owner value
passed in to the attribute owner, and sets the balance to 0. Note we refer to the owner attribute
as “this dot owner”. “This” is an implicit parameter, which
is the object on which the method was called, or, in this
case, the object being constructed. Many Java programmers omit the use
of this, but I find it makes the code more
readable, and, if you have a sophisticated
development environment, it will suggest things for you after you
type “this dot”, which makes the typing a lot faster
and less error-prone. Next we have a couple simple methods
that just return the values of the owner and balance
attributes. After this, we have methods to
deposit or withdraw money. Since it doesn’t make sense to
deposit a negative amount, we throw an exception if the parameter
amount is negative; otherwise, we simply add it to the
balance. Similarly in withdraw, it is an error to
attempt to withdraw a negative amount, or an amount which exceeds the
current account balance. It’s always a good idea to check and make sure parameters to a method
match any assumptions that you have, especially when you are working with
other programmers. Finally, we have a short method that
returns a string containing the account owner and
balance. Now let’s look at a main program that
uses this class. In DemoBankAccount, we create two
bank account variables, account one and account two. At this point, there aren’t any bank
accounts yet—we have to call “new” for that. We call the constructor to create an
account for “Martin Carlisle”, then deposit fifty dollars. Then, we construct an account for
Mickey Mouse and add one hundred thousand dollars
(he is a bit richer after all!) Next we make withdrawals from each
account and print the results of calling
toString on each account. Note that each account had its own
owner and balance, even though they were created from
the same blueprint, BankAccount.

Leave a Reply

Your email address will not be published. Required fields are marked *