Object Oriented Programming

What is OOP?

Object Oriented Programing is a style of programing that uses objects that interact with each other.  These objects have properties and methods(behaviors).  Objects are created from classes, which are the templates to creating objects.  They define the properties, and we assign them when creating the object.  The objects become like actors in our digital world, they can be created and destroyed and act like robots or AI, or game pieces, or game boards, or interactive elements.

A real world example

As always, it is best to consider these ideas in real world examples before we think about them in the computer programing sense, so let’s think of something that there are many of, birds.

The Bird class

  • Properties
  • Behaviors
    • function hungry();
      • Check the stomach level
      • if the stomach level is below x set hungry to try
      • if hungry eat()
    • function eat();
      • use flying() to find Food Preference
      • consume food
      • when food is gone use hungry()
    • function flying();
      • Start flying animation, change position
    • function Singing()
      • randomly play song file

 

With this class we can create many kinds of birds to live in our world

Colibri-thalassinus-001-edit-1

Bird hummingbird = new Bird(
Appearance = hummingbird.jpg;
Food = Nectar;
Song = twitter.mp3;
Flying Style = fastflying.mov;
hungry = false)

and

Pygoscelis_papua-1

Bird penguin = new Bird(
Appearance = penguin.jpg;
Food = fish;
Song=quack.mp3;
Flying Style = swim.mov
hungry=true)

now we just need some food to feed them with

The Basic Layout of a Class in Processing

these things are case sensitive

class ClassName{
The class Name
int a, b, c;
 boolean isthis,isthat;
The Properties, these can be combined if they are the same kind of variable type
ClassName(temp vars){
properties=tempvars
}
The Constructor, the function that is called to create the objects.  It acts like the setup function in the main document.  So this is where you would assign values to properties, or set up shapes or other objects that are associated with the object
void update(){}
void createShape(){}
These are examples of methods you would find in an object
;}
 finish the class with another bracket to close the one from the first line

 

Now to create the object from this class we simple call

ClassName object = new ClassName(temp vars)

because the variables that are going into the constructor are local to the constructor, it is important to realize that they will only be available to the constructor until you assign them to the properties of the class.

The ‘Hello World’ Example

This will be the most basic example ever

Msg m;
void setup(){
  m = new Msg();
}

void draw(){
}

class Msg{
  Msg(){
    println("hello world");
  }
}

It simply creates an object that outputs ‘hello world’.  But that does nothing for us, except constantly print hello world.  Lets make it a little more useful.

Msg m1,m2,m3;
void setup(){
  m1 = new Msg("hello world");
  m2 = new Msg("I am an object");
  m3 = new Msg("I am also an object");
}

void draw(){
}

class Msg{
  Msg(String s){
    println(s);
  }
}

Now we have created a class that makes talking objects, but the real power in this are things that are a little more complicated.This class seems to be used a lot for examples on the processing site and elsewhere.

class Ball{
  int xpos, ypos, radius;
  Ball(int tx, int ty, int tr){
    xpos=tx;
    ypos=ty;
    radius=tr;
  }

  void update(){
    ellipse(xpos,ypos,radius*2,radius*2);
  }
}

Now to build the ball in our sketch we use the following in the main document

Ball b;
void setup(){
  b=new Ball(width/2,height/2,20);
}

void draw(){
  b.update();
}

Adding to the Ball class, we can make it move by incrementally adding or subtracting from the xpos or ypos properties

class Ball{
  int xpos, ypos, radius, speed;
  Ball(int tx, int ty, int tr){
    xpos=tx;
    ypos=ty;
    radius=tr;
  }

  void update(){
    ypos+=speed;
    ellipse(xpos,ypos,radius*2,radius*2);
  }
}

The following code above, breaks because we have not set the speed property of the class, we can set that in the constructor, like the rest of the properties, or we can set it separate by just assigning it directly like b1.speed=3. more below

Ball b1, b2;
void setup(){
  size(200,200);
  b1=new Ball(width/2,height/2,20);
  b1.speed=1;
  b2=new Ball(30,0,10);
  b2.speed=3;
}

void draw(){
  background(255);
  b1.update();
  b2.update();
}

class Ball{
  int xpos, ypos, radius, speed;
  Ball(int tx, int ty, int tr){
    xpos=tx;
    ypos=ty;
    radius=tr;
  }

  void update(){
    ypos+=speed;
    ellipse(xpos,ypos,radius*2,radius*2);
  }
}

Now you can see I’ve created two objects from the class and they both fall at different speeds.  We can create many more objects that have slightly different properties.


Comments

One response to “Object Oriented Programming”

  1. […] you are new to Object Oriented Programming you might go ahead and make sure you can make this class into an object.  I would generally define […]

Leave a Reply