You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion, Adobe Flex

ColdFusion: Article

Inheritance in ColdFusion: Code Reuse for CFCs

When it comes to code, less is more

But how are a CFC's properties and methods created? When the instance of the CFC is created, the "pseudo-constructor" of the CFC is called. Effectively, this means that all the code in the CFC that defines methods is used to create the object's methods and that all other code in the CFC, including any code that creates the object's properties, is executed.

Once an object has been created from a CFC, it and its public properties and methods can be referenced. By assigning the object to a variable, it and its public properties and methods can be referenced an unlimited number of times during the life of that variable. In line 1 of callsimple.cfm, we see that the object created from simple.cfc is assigned to a variable named myObj. Because no scope is declared for myObj, it is placed in the Variables scope. On the fourth line of callsimple.cfm, we see the public myVar property of the myObj instance of simple.cfc being referenced, and on the fifth line of callsimple.cfm, we see the public myMethod method of that same myObj instance of simple.cfc being called. Referencing an object's public properties and methods is as simple as using the variable name to which the object was assigned followed by a period (a.k.a. "dot") and then the name of the property or method.

When calling a method of an object, we also must include a pair of parentheses after the method name. Inside those parentheses we must include, if any for the given method, a comma-delimited list of the arguments to be passed to the method. There are other ways to pass arguments to methods, but for simplicity's sake we won't cover those in this article.

Now that we know what is going on in callsimple.cfm, let's look at its output:

text output

The first line of the output results from simply creating an instance of simple.cfc on the first line of callsimple.cfm and the resultant pseudo-constructor execution of all code in simple.cfc. The second line of the output results from referencing the public myVar property of myObj on the fourth line of callsimple.cfm. Finally, the third line of the output results from referencing the public myMethod method of myObj on the fifth line of callsimple.cfm.

With a basic understanding of CFCs in place, let's move on to the purpose of this article: reusing code with CFCs. As mentioned earlier, we can reuse code in CFCs not only via all the standard CFML code reuse methods - including files, calling custom tags, and calling user-defined functions - but also via inheritance. But what exactly is inheritance?

CFCs and Inheritance
In the real world, inheritance involves one entity gaining things as the result of a relationship with another entity. Inheritance in CFCs is very similar: one CFC gains things (specifically, CFML code) as the result of a relationship with another CFC. We call a CFC that inherits from another a "child CFC" and we call the CFC from which that child inherits a "parent CFC." CFML inheritance is virtual, meaning both the child CFC and the parent CFC contain the complete CFML code of the parent CFC, without duplication of the CFML code of the parent CFC in the child CFC file.

Why is inheritance useful? Because it allows us to write code in one CFC and then use that code in numerous other CFCs. Thus, we can write that CFML code one time and have it virtually contained in an unlimited number of CFCs through inheritance. By writing that CFML only one time, it's less likely we will write bugs into it and, should a bug be discovered in it or should a requirement for its functionality change, we need only modify a single instance of the code.

Now that we have an understanding of CFC inheritance, let's take a look at how it's used in a file called exsimple.cfc:

<cfcomponent extends="simple">
    <cffunction name="newMethod">
       <cfreturn "newstring">

Note the difference between the opening <cfcomponent> tag of simple.cfc and the opening <cfcomponent> tag of exsimple.cfc - exsimple.cfc has 'extends="simple"' in that opening <cfcomponent> tag. The extends attribute of a CFC's <cfcomponent> tag is used to specify that CFC's parent. As such, by using 'extends="simple"' in the opening <cfcomponent> tag, we cause exsimple.cfc to virtually contain all the CFML in simple.cfc. Thus, when an instance of exsimple.cfc is created with either the CreateObject() function or the <cfobject> tag, the pseudo-constructor of simple.cfc is called, causing the CFML code contained in simple.cfc to be executed, just as if that code were located in exsimple.cfc. In addition, after an instance of exsimple.cfc has been created, we can reference on that instance any of the public properties and methods defined in simple.cfc just as if they were defined in exsimple.cfc. Let's see what this looks like in a file named callexsimple.cfm:

<cfset myObj = CreateObject("component", "exsimple")>

We create an instance of exsimple.cfc. Because exsimple.cfc extends simple.cfc, we can reference the public myVar property and the public myMethod method of simple.cfc as well as the public newMethod method of exsimple.cfc. The output of callexsimple.cfm is exactly what we expect:

text output

Now if we update simple.cfc, exsimple.cfc will automatically get the benefits of that change without being changed at all.
Because we didn't have to duplicate code from simple.cfc in order to use it in exsimple.cfc, we were able to write less code. Less code is better code because it gives us fewer places to introduce bugs and because it's easier to maintain.

More Stories By Josh Adams

Josh Adams is the developer evangelist for New Atlanta's BlueDragon family of CFML application server products. He presents on a regular basis at technical conferences and user groups throughout North America. Josh is also an active CFML developer both in his role at New Atlanta and in other endeavors.

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.

IoT & Smart Cities Stories
Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a m...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
Whenever a new technology hits the high points of hype, everyone starts talking about it like it will solve all their business problems. Blockchain is one of those technologies. According to Gartner's latest report on the hype cycle of emerging technologies, blockchain has just passed the peak of their hype cycle curve. If you read the news articles about it, one would think it has taken over the technology world. No disruptive technology is without its challenges and potential impediments t...
If a machine can invent, does this mean the end of the patent system as we know it? The patent system, both in the US and Europe, allows companies to protect their inventions and helps foster innovation. However, Artificial Intelligence (AI) could be set to disrupt the patent system as we know it. This talk will examine how AI may change the patent landscape in the years to come. Furthermore, ways in which companies can best protect their AI related inventions will be examined from both a US and...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (November 12-13, 2018, New York City) today announced the outline and schedule of the track. "The track has been designed in experience/degree order," said Schmarzo. "So, that folks who attend the entire track can leave the conference with some of the skills necessary to get their work done when they get back to their offices. It actually ties back to some work that I'm doing at the University of San...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...