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.

Note:

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.

Shape.cfc

abstract component Shape{
	abstract function draw();
} 

Square.cfc

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

ShapeFactory.cfc

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

Rectangle.cfc

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

AbstractFactory.cfc

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

FactoryProducer.cfc

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

index.cfm

<cfscript>
   //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
   shape2.draw();
   //get an object of Shape Square 
   shape3 = new shapeFactory().getShape("SQUARE");
   //call draw method of Shape Square
   shape3.draw();
</cfscript>

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,

component{
	final max=100;
		
	function mymethod(){
		//max=100;
		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.

component{
	final max;
	// constructor
	
	function mymethod(){
		max=50;
		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,

A.cfc

final component {
}

The code below produces an exception:

B.cfc

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,

Test.cfc

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");
	}
}

Demo.cfc

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.

main.cfm

<cfscript>
	d1=new Demo();
	d1.display();
	d1.show();
</cfscript>

Output

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.

<cfscript>
	myobj=new Comp();
	writeOutput(myobj.returnsany("hello world"));
</cfscript>

Covariance

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

In ColdFusion (2018 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, Animal.cfc.

    interface {
    	IPerson function returnsAny();
    	void function acceptAny(required string x, required numeric y, required numeric z);
    
  2. Define an interface, IPerson.cfc.

    interface {
    }
    
  3. Define an interface, CPerson.cfc, that implements the interface defined in I.cfc.

    interface extends="IPerson"{
    }
    
  4. Define an interface, Person.cfc, that extends CPerson, and defines the properties of the object defined in ReturnsString.cfc.

    component displayName="Person" implements="CPerson"{
        this.firstName="John";
        this.lastName="Doe";
    }
    
  5. Define the functions, returnsAny and acceptAny, in ReturnsString.cfc.

    component implements="I"{	
      
    	  Person function returnsAny(){
    	    obj1 = createObject("component", "Person");
    	    return obj1;
                }	
    
    void function acceptAny(required string a,required numeric b,required numeric c)
    {	
             WriteOutput(a);
             WriteOutput(b);
             WriteOutput(c);
    } 
    }
    
  6. Finally, create an index.cfm, that calls the component, ReturnsString, and further invokes the function, returnsAny.

    <cfscript>
    	writeDump(invoke("ReturnsString", "returnsAny"));
    </cfscript>
    

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy