User Guide

Table Of Contents
Using CFCs effectively 231
Using component inheritance
Component inheritance lets you import component methods and properties from one
component to another component. Inherited components share any component methods or
properties that they inherit from other components, and ColdFusion MX initializes instance data
in the parent CFC when you instantiate the CFC that extends it.
When using component inheritance, inheritance should define an is a relationship between
components. For example, a component named president.cfc inherits its methods and properties
from manager.cfc, which inherits its methods and properties from employee.cfc. In other words,
president.cfc is a manager.cfc; manager.cfc is an employee.cfc; and president.cfc is an
employee.cfc.
In this example, employee.cfc is the base component; it’s the component upon which the others
are based. The manager component extends the employee component; it has all the methods and
properties of the employee component, and some additional ones. The president component
extends the manager component. The president component is called a subcomponent or child
component of the manager component, which, in turn, is a child component of the employee
component.
To use component inheritance:
1.
Create the employee.cfc file with the following content:
<cfcomponent>
<cfset This.basesalary=40*20>
</cfcomponent>
2.
Create the manager.cfc file with the following content:
<cfcomponent extends="employee">
<cfset This.mgrBonus=40*10>
</cfcomponent>
In the example, the cfcomponent tags extends attribute points to the employee component.
3.
Create the president.cfc file with the following content:
<cfcomponent extends="manager">
<cfset This.prezBonus=40*20>
</cfcomponent>
In the example, the cfcomponent tags extends attribute points to the manager component.
4.
Create the inherit.cfm file with the following content, and save it in the same directory as the
components you created in the previous steps:
<cfobject name="empObj" component="employee">
<cfobject name="mgrObj" component="manager">
<cfobject name="prezObj" component="president">
<cfoutput>
An employee's salary is #empObj.basesalary# per week.<br>
A manager's salary is #mgrObj.basesalary + mgrObj.mgrBonus# per week.<br>
A president's salalry is #prezObj.basesalary + prezObj.mgrBonus +
prezObj.PrezBonus# per week.
</cfoutput>