Skip to content

Classes to objects

September 1, 2011

Classes are blueprints.  Objects are copies of the blueprints. This is a very fundamental concept to OOD/OOP.  Virtually all the entities (nouns) that you will create/encounter in the problem domain will be represented by a class.  Classes represent things that you see and can describe. Remember the definition of a noun? A person, place or thing.  A class is very similar.

Analogies:

A creature in a shooter game is a class, the creatures who chase after you while you are playing are objects.

A cookie cutter is a class – from it can be produced lots of cookies.  The cookies themselves are objects, while the cutter is the class.  A vacation request form is a class.  Filled out vacation request forms are the objects.  A recipe is a class, the roast from the oven that followed the recipe is an object.

Objects are implementations of the class.  The implementation of the class (object) has state.  That means it has data that is unique to it, that describes it.  For example the cookie cutter has 3 cookies that came from the class, each has its own size, taste, and other attributes (fields) that describe it.  The state of the object is unique to each object, yet all follow the design of the class.

In Java, you create a class like this:

public class Zombie{

public String type;

public  int size;

public  int speed;

public  int damage ;

}

Then instances of the class like this:

Zombie one = new Zombie;

Zombie two = new Zombie;

Zombie three = new Zombie;

This creates 3 instances of the Zombie class, each with its own *copy of the attributes or fields type, size, speed and damage.

Whenever a new zombie appears or spawns during game play, it is spawning from the Zombie class.  Each spawned Zombie is an object instantiated from the Zombie class.

If you prefer baking to shooting, here is another example.

In Java, you create a class like this:

public class Cookie{

public String type;

public  String size;

public  String frosting;

public  int bakeTime;

}

Then instances of the class like this:

Cookie peanutButter = new Cookie;

Cookie suger = new Cookie;

Cookie chocolateChip = new Cookie;

This creates 3 instances of the Cookie class, each with its own *copy of the attributes or fields type, size, frosting and bakeTime.

Advertisements

From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: