Object Oriented Programming in ColdFusion

To learn about ColdFusion components, see Build and use ColdFusion components.

Read on to know the new and enhanced OOP features in ColdFusion.

Abstract components and methods

An abstract component can have abstract method without body and can also have methods with implementation.

Use the abstract keyword to create an abstract component and method. In ColdFusion, you cannot instantiate an abstract component. An abstract component is mostly used to provide base for sub-components. The first concrete component should have the implementation of all the abstract methods in its inheritance hierarchy.


A concrete component cannot have abstract methods.

To declare an abstract component,

// myAbstractClass.cfc
abstract component {
 // abstract methods
 // concrete methods

That is all there is to when declaring an abstract component in ColdFusion.

You cannot create instances of myAbstractClass. Thus, the following code is NOT valid:

myClassInstance=new myAbstractComponent();

Here is an example of using abstract components in ColdFusion.


abstract component Shape{
 abstract function draw();


component Square extends="Shape" {
 function draw() {
      writeoutput("Inside Square::draw() method. <br/>");


component ShapeFactory extends="AbstractFactory" {
 Shape function getShape(String shapeType){
       if(shapeType EQ "RECTANGLE"){
         return new Rectangle();
      }else if(shapeType EQ "SQUARE"){
         return new Square();
         return "not defined";


component Rectangle extends="Shape" {
   function draw() {
      writeoutput("Inside Rectangle::draw() method. <br/>");


abstract component AbstractFactory {
     abstract Shape function getShape(String shape) ;


component FactoryProducer {
 AbstractFactory function getFactory(String choice){
   if(choice eq "SHAPE"){
         return new ShapeFactory();


   //get shape factory
   shapeFactory =  new FactoryProducer().getFactory("SHAPE");
   //get an object of Shape Rectangle
   shape2 = new shapeFactory().getShape("RECTANGLE");
   //call draw method of Shape Rectangle
   //get an object of Shape Square 
   shape3 = new shapeFactory().getShape("SQUARE");
   //call draw method of Shape Square

Final component, method, and variable

In ColdFusion, inheritance is a highly useful feature. But at times, a component must not be extended by other classes to prevent data security breaches. For such situations, we have the final keyword.

ColdFusion supports the following:

  • Final component
  • Final method
  • Final variable

Final variable

Final variable are constants. Use the final keyword when declaring a variable. You cannot change the value of a final variable once you initialize it. For example,

 final max=100;
 function mymethod(){
  writeoutput("final variable is: " & max);

Create an index.cfm, instantiate the component, and call the method defined in the component. If you change the value of max in the method, you see an exception. The following snippet also produces an exception.

 final max;
 // constructor
 function mymethod(){
  writeoutput("final variable is: " & max);

Final component

You cannot extend a final component and override a final method. A final component, however, can extend other components.

To define a final component, see the example below:

final component{
 // define methods

To define a final method, use the specifier before the final keyword and the return type. For example,

public final void function myMethod() {
 // method body

A final component helps secure your data. Hackers often create sub-components of a component and replace their components with the original component. The sub-component looks like the component and may perform a potentially malicious operation, which would compromise the entire application. To prevent such subversion, declare your component as final and prevent the creation of any sub-component. For example,


final component {

The code below produces an exception:


component extends=”A” {

Final method

A method with a final keyword cannot be overridden. This allows you to create functionality that cannot be changed by sub-components. For examples,


component {
 // final method and cannot be overridden
 public final void function display(){
  writeOutput("From super class, final display() method");
 // concrete method
 public void function show(){
  writeOutput("From super class, non-final show() method");


component extends="Test"{
 //public void function display(){}
 public void function show()
     writeOutput("From subclass show() method");

In Demo. cfc , uncommenting the above line produces an exception, since you cannot override a Final method. You can, however, override a non-final method, as shown in the code above.


 d1=new Demo();


From super class , final display() method

From subclass show() method

Default functions in interfaces

This feature enables you to declare a function in an interface, and not just the signature. While extending the interface, developers can choose to either override or reuse the default implementation.

Default Functions enables extending the interfaces without breaking old applications.

For example, create an interface, I.cfc, that defines a function returnsany, which returns an object of any type. Use the keyword "default", while defining such functions.

interface {
 public default any function returnsany(any obj)
  return obj;

Create a cfc, comp.cfc, that implements the interface, I.cfc.

component implements="I"


Now, create on object that returns a string.

 myobj=new Comp();
 writeOutput(myobj.returnsany("hello world"));


Covariance is the relationship between types and subtypes. In ColdFusion (2021 release), Covariance is supported for return types in overriding a method.

In ColdFusion (2021 release), a return type is covariant in the component implementing the interface. The example below shows how you can leverage covariance.

The argument name is not compared during interface implementation, i.e, if the interface function takes argument name as x , the implementing function in the component can have any argument name.

  1. Define an interface, IAnimal.cfc.

    interface displayName="IAnimal" hint="Animal Interface."{
        any function eat(any prey="");
        any function makeNoise(string noise);
  2. Define component, Animal.cfc, which implements the Interface.

    While implementing the interface function eat() & makenoise (), it overrides the return type to a more specific type. This is known as Covariant Method Return Type.

    component implements="IAnimal" displayname="Animal"{
     property animalSize;
     property animalType;
     function init(animalSize,animalType){
      variables.instance.animalSize = arguments.animalSize
      variables.instance.animalType = arguments.animalType
     Animal function eat(any prey=""){
       return prey;
     string function makeNoise(string noise){
       return noise;
     function getanimalSize(){
      return variables.instance.animalSize
     function getanimalType(){
      return variables.instance.animalType
  3. Create index.cfm and check how the animal eats and makes noise.

     bunny=new Animal('small','rabbit');
     simba =new Animal('large','Tiger');
     writeoutput("Simba eats " & simba.eat(bunny).getAnimaltype() & "<br/>");
     writeoutput("Simba " & simba.makenoise('roarsss'));


Get help faster and easier

New user?