Where we've been
We have been exposed to using both primitive (int, double, boolean, char) and reference (Scanner, String, ArrayList) type variables. Until now, we have worked with a limited knowledge of reference type variables. Object oriented programming is the process of creating your own reference type variables.
What is an Object?
An object is a representation of any real world thing. For example, civil engineers working on simulating traffic flow might need to represent cars and traffic lights. Game designers might need to represent missals and attackers. Cell phone developers often need to reference dates, times and locations (GPS coordinates). Each of these items would be an object in Java and we would develop classes for each of them.
Class vs. Object
The words class and object are often thrown around in Java. The difference is subtle, but important. An object is an instance of a class. Think of it this way – My wife loves to bake. Most often, it is cakes or cookies. We have two storage bins of cake pans and cookie cutters, some for each holiday. So, when she makes her famous lemon cookies at Christmas, she often uses a snowman cookie cutter. My kids then decorate each snowman in their own way using color sugar. When each snowman cookie comes out of the cutter, they are identical. In this case, the cookie cutter is the class and the snowmen cookies are the objects.
Another way to look at it can be found in construction. When a new neighborhood is built, there is a set of blueprints that the construction company must follow to build the houses. Each house that is built by these blueprints is built the same way. Which is the class, the blueprint or the house? You guessed it, the blueprint, or the plan, is the class. And, each house is an instance of the blueprint (also known as an object).
In Java, the class is the code file that creates the properties and actions that the object will need. The properties are stored as variables and the actions are written as methods. The object is then the named variable of the class type. For example, in the statement Car myCar = new Car(). The class is car and the object is myCar.
Designing Object
Objects have two main parts – properties and actions. Properties qualities that an object possesses and actions are things that the object can do. The designer of a class needs to determine what properties and actions the objects of the class will need to perform the necessary jobs. In the case of the car object that the civil engineers need, we might store properties of make, model, year, color, and number of passengers. Meanwhile, each car will need to move forward, backward, stop and turn (both right and left).
We often use something called UML (Unified Modeling Language) to display the design of a class. UML is a comprehensive set of rules for designing and modeling, but will only use a subset of this information. In a basic UML diagram, we will need to state the variables with types (properties) and the methods with return types and parameters (actions). We will also need to denote whether each of these items are private or public (to be discussed later).
So, for our car class, the resulting UML diagram looks as follows:
Car |
String Color; String make; String model; int year; int numPassengers; |
void moveForward(double distance)
void moveBackward(double distance)
void turn(int degrees)
void stop( ) |
Coding a Class
When we write the code for a class, we DO NOT create a main. A class is code that should not run by itself. From here on out, when you are asked to make a program, use a main, otherwise, it is a class with no main. So, to code our Car class we would enter the following:
/**
* The car class represents a car for a traffic simulation.
* @author mengel
* @version 2/6/12
*/
public class Car {
//instance variables
private String color;
private String make;
private String model;
private int year;
private int numPassengers;
//instance methods
/**
* Moves the car forward dist
* @param dist the distance to move forward
*/
public void forward(double dist){ //to be coded later }
/**
* Moves the car backward dist
* @param dist the distance to move backward
*/
public void backward(double dist){ //to be coded later }
/**
* turns the car right or left
* @param degrees the amount of degrees to turn
*/
public void turn(int degrees){ //to be coded later }
/**
* stops the car
*/
public void stop(){//to be coded later }
}
Instantiating Objects & Accessing Variables
When we declare a variable in java, we give it a name and type:
int age;
String name;
Declaring a reference type variable is the same:
Scanner reader;
ArrayList list;
Car myCar;
When we initialize a variable, we give it a starting value:
age = 4;
name=”Jim”;
However, when we initialize a reference type variable, we use the keyword new. This helps to call a method that initializes ALL of the objects variables. This process is called instantiation (creating an instance).
reader = new Scanner(System.in);
list = new ArrayList ();
myCar = new Car();
To access the variables (or methods) of an object, once it has been instantiated, we use dot (.) notation. Dot notation will allow us to both set and get the values of the variable:
System.out.println(myCar.color);
myCar.make="Honda";