Welcome!

ColdFusion Authors: Maureen O'Gara, Hovhannes Avoyan, Yakov Fain, Pat Romanski, Liz McMillan

Related Topics: ColdFusion

ColdFusion: Article

What's an Object? An Introduction to OOP

An introduction to object-oriented programming for ColdFusion developers

To put it extremely simply, you already know what an object is. Objects are all around you. If you look at the computer you spend countless hours a day using, that will serve as a good example of what an object is in the real world. One of the amazingly powerful things about software objects is that they're designed to model real-world objects, and do quite a good job of it!

Your computer is an object, and as an object it has certain characteristics such as brand, processor type and speed, amount of RAM, hard drive size, etc. In object lingo, these characteristics are called attributes. Your computer can also do things (sometimes it doesn't do what you want it to do, but it's still doing something!). It can power up, launch applications, automatically run tasks such as a virus scan, and power down. In object parlance these things that your computer can do would be referred to as methods (or sometimes they're referred to slightly less accurately, at least from an OOP standpoint, as functions).

In addition to attributes and methods, perhaps the most important characteristic of an object is that it's self-contained. As I said earlier I'm not going to dive into the differences between procedural programming and OOP at this point because I think that's a bit difficult to understand without really understanding what an object is, but the notion of a self-contained object is important to address. In procedural programming there basically exists data and functions that work with this data, but the data and functions are typically separate and distinct from one another. With an object, the data (attributes) and functions (methods) are all contained within a single, tidy little software component known as the object. This offers numerous advantages that we'll delve into in a future article.

I'm a Person, Not an Object!
Let's consider another, very commonly used example to shed more light on the concept of objects, and this one you'll probably use more regularly in your applications: a person. A Person object, like the Computer object, also has attributes (for example, first and last name) and things they can do, or methods (for example, "walk"). So while you might not prefer to be thought of as an object, as far as the software world is concerned a person is a rather ideal example of an object. If you don't take too much offense let's proceed with this example for now.

Recalling that the three most important aspects of an object are attributes, methods, and that the object is self-contained, we're now going to look more specifically at how we'd go about modeling a person as an object in our applications. First, let's make a basic list of some of the more important attributes that a person has:

  • first name
  • last name
  • birth date
  • sex
  • height
  • weight
Now, let's make a list of some of the things a person can do. These translate into our methods:
  • walk
  • run
  • sleep
  • work
  • talk
  • write ColdFusion applications
You get the idea. Attributes and methods are what define an object.

A Touch of Class
Earlier I mentioned the term class, and before we go much further we need to touch briefly on what a class is. Now that you understand at least in a basic sense what an object is you'll grasp the concept of class pretty easily. A class can be thought of as a generic version of an object. Continuing with our Person object discussion, think of the Person class as a generic person. You don't know if this person is male or female, young or old, short or tall, it's just a nameless, faceless, abstract person.

In OOP you use this generic Person class as the blueprint for creating specific Person objects. The Person class knows that a person has the attributes and methods listed above but knows nothing specific about a particular person. In other words, the Person class knows that a person has a first name, but it doesn't know any particular person's first name. The Person object will know specific details about a particular person, such as "This particular Person's first name is Matt."

You create a specific Person object, which is also known as an instance of the Person class, by instantiating the class. This is a fancy OO way of saying, "Let's take this generic Person class and make it into a specific Person object." For argument's sake let's assume we want to create a Person object called "matt." (I could always use another one of myself to help me get things done.)

In terms specific to ColdFusion, objects are defined using ColdFusion Components (CFCs). The Person class would be a CFC called Person.cfc. To instantiate this class in ColdFusion, you would either use the cfobject tag or the CreateObject function as follows:

Using the cfobject tag:
<cfobject component="Person" name="matt" />

Using CreateObject (this could be within a cfscript block or you could do this within a
cfset tag as well):
matt = CreateObject("component", "Person");

There are some details you'll need to know regarding where ColdFusion looks to locate your CFCs, but don't concern yourself with the specifics of implementation at the moment. Just repeat this phrase over and over again: "I'm thinking in objects. I'm thinking in objects." We'll get to a specific implementation at the end of this article.

So at this point we have a specific Person object called matt with which we can start working. When the matt object is first created, however, it doesn't necessarily know anything about itself. There are a couple of standard ways to get data into our object. You might think we can just start setting our object's attributes using dot notation as follows:

matt.firstName = "Matt";
matt.lastName = "Woodward";

More Stories By Matthew Woodward

Matt Woodward is Principal Information Technology Specialist with the Office of the Sergeant at Arms at the United States Senate. He was until recently a Web application developer for i2 Technologies in Dallas, Texas. A Macromedia Certified ColdFusion Developer and a member of Team Macromedia, he has been using ColdFusion since 1996. In addition to his ColdFusion work, Matt also develops in Java and PHP.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.