Objects
Java is Object-oriented
Java is object-oriented, which allows
overall program design to focus on what is being dealt with rather than
on how the program operates. For example, a car race game application
would be designed using Car objects – where each Car object has specific
attribute values and actions which it can perform. A banking program would be designed using
Account object, where each Account object would have attributes (perhaps a
name, account number and balance), and be able to perform actions upon
itself (perhaps deposit, withdraw, check balance).
An object-oriented design is most useful
for programming in sophisticated projects because one can break things down
into understandable pieces.
Additionally, these pieces can easily be reused in another project,
reducing the cost of software development.
What is an Object?
By definition, an object is an entity with
attributes and behavior. That means that
an object has specifics by which it can be described (attributes) and can
perform actions (behavior). The world
is comprised of objects.
For example, many people have a car. Cars are objects. A car has attributes by which it can
be described: it’s color, make, model,
quantity of gas it holds. A car
also has actions it can perform:
drive forward, drive backward, fill gas, display gas level.
Joe and Mary both have cars, which are
described in the object diagrams below.
Note that both objects have the same attributes, but each object
has different values for those attributes.
Both cars can perform the same actions.
Joe’s car |
|
Mary’s car |
color = blue make = Honda model =
Accord gas = 20.5 |
|
color = red make = Ford model = Taurus gas = 35.1 |
Joe’s car and Mary’s car are objects of the
same type. That is, they have the same
attributes and the same behavior (as opposed to, say, a boat). Joe’s car and Mary’s car are objects of the
same class.
Another example would be a
dog. I have a dog, my neighbor has a dog. All dog’s have
a breed, a weight, a name. These attributes of a dog have values which describe the
dog. Dog’s can perform actions such a run, sleep, eat.
My dog and my neighbor’s dog are both object of
the same type, or class.
What is a Class?
A
class is a template, or blue print, which describes an object. It specifies the attributes and actions which
are associated with a particular type of object. In Jave,
once a class has been defined, one or more objects of that class type can be created.
Let’s suppose that we were
writing a program which was going to manipulate cars. Our program would need to be able to create
and manipulate Car objects. The program would need to define a Car class. Then objects of this type (say Joe’s car and
Mary’s car) could be created.
The following code creates
a simple Car class. Don’t
worry if you don’t understand everything in this example, just get the
general idea:
public class Car {
// class variables (attributes)
private String color;
private String make;
private String model;
private double gas;
// class methods (behaviors)
public Car (String col, String mk, String mod, double galofgas){
color = col;
make = mk;
model = mod;
gas = galofgas;
}
public void moveForward(double miles){
gas = gas -
miles/18.0;
}
public void moveBackward(double miles){
gas = gas -
miles/18.0;
}
public void fillGas(double
gallons) {
gas = gas + gallons;
}
public double howMuchGas () {
return gas;
}
} //end class Car
The Car class above
indicates that each object of type car:
·
has
a specific color, make, model, and quantity of gas
·
can
be created if the above attribute values are provided
·
can
move forward
·
can
move backward
·
can
be filled with more gas
·
can
report the quantity of gas it has
The Car class above is
not a Car object,
but only describes what variables (attributes) and methods (actions) are associated with a
Car object, if one is created.
To actually create a Car
object (an instance of the Car class) and use it’s behavior we need to
declare a variable of class Car, create
the Car and then call upon it’s methods:
//create marys
car object
Car marys;
marys = new Car(
“red”, “Ford”, “Taurus”, 35.1 );
// create joes car object
Car joes;
joes = new Car( “blue”,
“Honda”, “Accord”, 20.1 );
//these objects now perform actions
marys.moveForward(17);
joes.moveBackward(.002);
joes.fillGas(15.0);
System.out.println(“Mary’s
gas level is” + marys.howMuchGas() );