OOP Terminology : Part III
August 11, 2008
The Basics
Class (also known as a Component)
In ColdFusion a class is a cfc defined with the cfcomponent tag. A class is an object that contains the functions (called methods) which represent what the the object can do or knows about. A class should be considered as a data type that describes an object. Classes contain both data, and the methods for acting on the data.
Constructor (also known as the init method)
In most OO languages, concrete classes have a constructor, which is a special method that initializes the object. ColdFusion doesn't have constructors, instead the community have adopted the init method. Coldfusion also has the concept of a pseudo constructor.
Pseudo Constructor
Any ColdFusion code that you put in your CFC that is not inside a method (cffunction code block) will be run when the CFC is instantiated. This is known as the pseudo constructor.
Instance Variables (also known as private variables)
An instance variable is a private/local variable that is created and exist within the class. In other languages these are sometimes called private variables.
Object
An object is an instance of a class. The instance exists in memory.
Types of Class
Concrete Class
A concrete class is technically speaking a class that can be initiated. As ColdFusion doesn't have a constructor (method that is automatically called when then class is loaded), we can think of it as a class that has an init method that returns itself.
Abstract Class
An abstract class can not be initiated so it has no constructor/init method. The purpose of an abstract class is to be inherited by another class (using the extends attribute of the cfcomponent tag). An abstract class can also be thought of as a super class. A good example of how to think of an Abstract Class would be a Shape Class. A shape doesn't actually define what the shape looks like, it more of a concept - an abstract concept of a shape. We would use a Concrete Class, such as square or triangle, to define what the shape looks like. If you find yourself creating a object by calling a CFC, then it is not an abstract class. The methods inside an abstract class are abstract methods and should not be called directly. In Java, it will actually throw an error if you try to call an abstract class.
Abstract Class "aShape.cfc"
<cfcomponent hint="I am an abstract class">
<cffunction name="setColour">
<cfargument name="hex"
type="string"
required="true"
hint="I am the colour as a hex string" />
<cfset variables.instance.colour = arguments.hex />
</cffunction>
<cffunction name="getArea"
returntype="void"
hint="I am an abstract method that must be defined in a concrete class">
</cffunction>
</cfcomponent>
Concrete Class "Rectangle.cfc"
<cfcomponent extends="aShape"
hint="I am a concrete class defining a rectangle">
<cffunction name="init"
returntype="Rectangle"
hint="I am the pseudo constructor">
<cfargument name="width"
type="numeric"
required="true"
hint="I am the width of the rectangle" />
<cfargument name="height"
type="numeric"
required="true"
hint="I am the height of the rectangle" />
<cfset variables.instance = StructNew() />
<cfset setWidth( arguments.width ) />
<cfset setHeight( arguments.height ) />
<cfreturn this />
</cffunction>
<!--- accessors (getters and setters) --->
<cffuncttion name="getArea">
<cfreturn variables.instance.height * variables.instance.width />
</cffunction>
<cffunction name="setWidth"
returntype="void"
hint="I set the width of the rectangle">
<cfargument name="width"
type="numeric"
required="true"
hint="I set the width of the rectangle" />
<cfset variables.instance.width = arguments.width />
</cffunction>
<cffunction name="setHeight"
returntype="void"
hint="I set the height of the rectangle">
<cfargument name="height"
type="numeric"
required="true"
hint="I set the height of the rectangle" />
<cfset variables.instance.height = arguments.height />
</cffunction>
... more methods...
</cfcomponent>
Base Class
A base class is a class that is inherited. Also known as a Super Class.
Derived Class
A class that inherits from a Base Class. This is also known as a Sub Class. The derived class inherits all of the member variables and methods of the base class from which it is derived.
The Class of '94
Your school reunion.
Interface
The cfinterface tag is new in ColdFusion 8! An interface can be thought of as a blueprint for any class that implements it. It defines the methods, their arguments and data types for all classes that implement it. The methods (function) defined in the interface must exist in the classes that implement it. To make a class implement an interface in ColdFusion you use the implements attribute of cfcomponent. It is worth knowing that a class can implement more than one interface by using a comma delimited list. It is generally considered to good practice to prefix your cfcs that are interfaces with an "I". For example IVehicle.cfc. Your interface should not contain an init method.
There has been a far amount of debate as to the value of interfaces in a loosely typed language like ColdFusion. I personally think they are very useful in complex applications to define common methods and data types for all classes that implement it. In other words if you know how to use one class you know a whole lot about the other classes safe in knowledge that it won't change.
An example of using an interface.
IVehicle.cfc
<cfinterface displayname="IVehicle">
<cffunction name="speedUp"
hint="I define a speedUp method in classes that implement this interface"
access="public"
output="false"
returntype="numeric">
<cfargument name="rate"
type="numeric"
hint="I define an argument for this method in classes that implement this interface"
required="true" />
</cffunction>
<cffunction name="brake"
hint="I define a brake method of classes that implement this interface"
access="public"
output="false"
returntype="numeric">
<cfargument name="rate"
type="numeric"
hint="I define an argument for this method in classes that implement this interface"
required="true" />
</cffunction>
</cfinterface>
Hovercraft.cfc
<cfcomponent implements="IVehicle">
<cffunction name="init"
returntype="Hovercraft"
hint="I am the pseudo constructor">
<cfargument name="speed"
type="numeric"
required="false"
default="0"
hint="I am the default speed" />
<cfset variables.instance = StructNew() />
<cfset variables.instance.speed = arguments.speed />
<cfreturn this />
</cffunction>
<cffunction name="speedUp"
hint="I increase the speed of the car"
access="public"
output="false"
returntype="numeric">
<cfargument name="rate"
type="numeric"
hint="I am the amount to increase the current speed"
required="true" />
<cfset variables.instance.speed = variables.instance.speed+arguments.rate />
</cffunction>
<cffunction name="slowDown"
hint="I decrease the speed of the car"
access="public"
output="false"
returntype="void">
<cfargument name="rate"
type="numeric"
hint="I am the amount to decrease the current speed"
required="true" />
<cfset variables.instance.speed = variables.instance.speed-arguments.rate />
</cffunction>
</cfcomponent>
Bicycle.cfc
<cfcomponent implements="IVehicle">
<cffunction name="init"
returntype="Bicycle"
hint="I am the pseudo constructor">
<cfargument name="speed"
type="numeric"
required="false"
default="0"
hint="I am the default speed" />
<cfset variables.instance = StructNew() />
<cfset variables.instance.speed = arguments.speed />
<cfreturn this />
</cffunction>
<cffunction name="speedUp"
hint="I increase the speed of the car"
access="public"
output="false"
returntype="numeric">
<cfargument name="rate"
type="numeric"
hint="I am the amount to increase the current speed"
required="true" />
<cfset variables.instance.speed = variables.instance.speed+arguments.rate />
</cffunction>
<cffunction name="slowDown"
hint="I decrease the speed of the car"
access="public"
output="false"
returntype="void">
<cfargument name="rate"
type="numeric"
hint="I am the amount to decrease the current speed"
required="true" />
<cfset variables.instance.speed = variables.instance.speed-arguments.rate />
</cffunction>
</cfcomponent>
Ways to use classes
Singleton
A singleton is when only one instance of a class exists and only one instance can exist at a time. In ColdFusion this is usually done by storing the object in the application scope.
Transient
If you need to have more than one instance of a class at a time, then you are using transient objects. For example you may have a shopping basket stored in your session scope. Each user would need their own instance.
Types of Method
In Coldfusion, methods in a class can be private, public or package.
Accessors describe methods of a class that get or set private data held in the variables.instance scope of the class. You will often hear these refered to as getters and setters.
- Posted in:
- ColdFusion
- OOP
6 comments
Leave a comment
If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

good posts, it's always interesting to see how others approach the OO world. :-)
I think that the explanation of the pseudo constructor, while very plausible, is not really accurate... actually all code that is written outside any function is treated as a pseudo constructor. When you do something like
<cfset myObj=createObject("component", "Square") />
all code that is outside the functions will immediately be executed, so in the example above the instance will already have a width and a height, although the init() function has not been called yet.
You are right about the init() function being adopted as a pseudo constructor, but, I would recommend to put _all_ code needed for instantiation in that function. Alternatively you could remove the function and place that code outside the functions.
Chris
Comment by Chris – August 11, 2008
I was trying to demonstrate the concepts rather than actual code, but you're absolutely right, I will update the post. It has also occurred to be that a square by definition must have the same width and height so I'll change it to a rectangle.
Thanks for your input :)
Comment by John Whish – August 11, 2008
Comment by Ben Nadel – August 11, 2008
Comment by John Whish – August 12, 2008
Thanks, John.
Comment by Julian Halliwell – August 15, 2008
Comment by John Whish – August 15, 2008