The Coder's Handbook
Object Oriented Programming
CLASSES AND OBJECTS
When we want to store information, we have used variables like int, float, or boolean. These are fine for simple pieces of data. But what if you want to represent something more complicated in your program - like a point, a circle, a spaceship, or a robot?
The solution is to define something called a class. A class is section of code that defines a set of data and behaviors for an object. You might make hundreds of objects that all belong to the same class, just like when we use int we can make many different integers.
Each object usually shares common data, but also can have its own methods. Consider the example to the right. Dogs have traits like size, age, and color (data) and can do things like eat, sleep, and run (methods).
Let's think of some other examples:
A class named Teacher --> objects named Malafarina, Brown, or George
A class named Avenger --> objects named Iron Man, Captain America, or Thor
A class named Wizard --> objects named Gandalf, Harry Potter, or Caleb Widoghast
WAIT, I'VE SEEN THIS BEFORE...
Remember RoboQuest?
During RoboQuest, each project would require you design a new type of Robot to solve a particular problem.
You'd design a special kind of robot to climb stairs, or another one to decorate a fountain.
Each time you wrote code for a Robot you were actually writing a class - you just didn't know it!
PARTS OF A CLASS
Data
Information that objects of this class stores
Methods
Things that this class can do
Constructor
A special method that is called when it is created.
This is like the setup() method but for a class.
AN EXAMPLE - REDBOX
This program is split up into two tabs - one for your main program that has setup() and draw(). The other tab defines the RedBox class.
This is just like how we first define method and then we can call it in other parts of our program.
Instead, we're defining a special variable called a class, and using it in our main tab.
Example - The Main Tab
Your main program will make two objects of class RedBox. Then every frame it tells them to draw themselves on the screen.
// Declare each box
RedBox one;
RedBox two;
void setup()
{
// Initialize each box by calling the constructor
one = new RedBox();
two = new RedBox();
}
void draw()
{
// Tell each box to display itself
one.render();
two.render();
}
The RedBox Tab
A RedBox has four pieces of data , a constructor, and a method to draw it. The word "render" isn't a keyword - it just means display.
class RedBox
{
float x;
float y;
float w;
float h;
RedBox()
{
x = random(0, width);
y = random
w = 20;
h = 20;
}
void render()
{
fill(255, 0, 0);
rect(x, y, w, h);
}
}
One of the best things about using classes is that it helps keep our code clean and organized. Notice how simple the main part of our program is? No numbers or coordinates! It just says, "Hey, Box, go do your thing!"
USING ARRAYS OF OBJECTS
Here's where it starts to get interesting. We can objects with arrays to make many instances of one class, but each one can have it's own data. This lets us display objects with different positions, colors, speed, or health in our program. All at the same time, with very little code!
Notice that we won't need to change the RedBox tab at all. The class doesn't care how many times you use it!
Example Revised - The Main Tab
// Declare the array of boxes
RedBox[] boxes;
void setup()
{
// Initialize the array of boxes
boxes = new RedBox[500];
// Initialize each box in the array
for(int i = 0; i < boxes.length; i++)
{
boxes[i] = new RedBox();
}
}
void draw()
{
// Tell every box in the array to draw itself
for(int i = 0; i < boxes.length; i++)
{
boxes[i].render();
}
}
Reminder: It's important to initialize both the array itself and every box in the array.
Imagine you work at a tennis ball manufacturer. You need to produce the tube (the array) and the actual tennis balls (the objects). If you only make the tube, your customers will be very unhappy.
If you don't initialize the objects, you'll get something called a NullPointerException which will crash your program.
RESOURCES
Chapter 8 - Object Oriented Programming
8.1 - Object Oriented Programming
8.2 - Defining a Class (Part I)
8.3 - Defining a Class (Part II)
9.3 - Using Objects with an Array
9.4 - Using Loops with an Array of Objects