DevLearningTools

MODULE 7 · LESSON 10

Inheritance

Extending one CFC from another with extends, calling the parent's methods with super, and why ColdFusion doesn't allow multiple inheritance.

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

Inheritance lets one component build on another — reusing its properties and methods, and adding or overriding what's different, instead of duplicating shared logic across several unrelated CFCs.

Learning Objectives

After completing this lesson, you'll be able to:

  • Extend a component with the extends attribute.
  • Call a parent method from a child component with super.
  • Explain why ColdFusion doesn't support multiple inheritance, and what to use instead.

extends — Reusing a Parent's Behavior

CFScript — Animal.cfc
component {

    property name="name" type="string";

    function init(required string name) {
        variables.name = arguments.name;
        return this;
    }

    function describe() {
        return variables.name & " makes a sound.";
    }

}
Tag Syntax — Animal.cfc
<cfcomponent>

    <cfproperty name="name" type="string">

    <cffunction name="init">
        <cfargument name="name" type="string" required="true">
        <cfset variables.name = arguments.name>
        <cfreturn this>
    </cffunction>

    <cffunction name="describe">
        <cfreturn variables.name & " makes a sound.">
    </cffunction>

</cfcomponent>
CFScript — Dog.cfc (extends Animal)
component extends="Animal" {

    function describe() {
        return variables.name & " barks.";
    }

}
Tag Syntax — Dog.cfc (extends Animal)
<cfcomponent extends="Animal">

    <cffunction name="describe">
        <cfreturn variables.name & " barks.">
    </cffunction>

</cfcomponent>

Using It

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

Dog automatically has init() and every property from Animal without redeclaring them — it only had to define what's different: describe().

Calling the Parent's Version with super

When a child overrides a method but still wants to run the parent's original logic too (not replace it entirely), super.methodName() calls the parent's version explicitly.

CFScript — Dog.cfc
component extends="Animal" {

    function describe() {
        return super.describe() & " Specifically, it barks.";
    }

}
Tag Syntax — Dog.cfc
<cfcomponent extends="Animal">

    <cffunction name="describe">
        <cfreturn super.describe() & " Specifically, it barks.">
    </cffunction>

</cfcomponent>

No Multiple Inheritance

A CFC can extend exactly one parent — ColdFusion doesn't support inheriting from two components at once, unlike some languages. When behavior from multiple unrelated sources is genuinely needed, the standard CFML answer is interfaces (a component can implement several at once) rather than extending several parents — covered in the Interfaces lesson.

Common Beginner Mistakes

Trying to extend two components at once

extends="A,B" isn't valid — a CFC can only have a single direct parent. Use interfaces for behavior that needs to come from multiple sources.

Overriding a method and forgetting super when the parent's logic still needs to run

Overriding describe() entirely replaces the parent's version. If the parent's behavior should still happen as part of the child's version, call super.describe() explicitly inside the override.

Best Practices

  • Only extend when there's a genuine "is-a" relationship (a Dog is an Animal) — don't use inheritance just to reuse a couple of unrelated helper methods.
  • Prefer composition (one CFC holding an instance of another) over a deep inheritance chain when the relationship isn't a clean "is-a."

Interview Questions

How many parent components can a single CFC extend?

Exactly one — ColdFusion doesn't support multiple inheritance.

What does super.methodName() do?

It calls the parent component's version of that method explicitly, typically used inside an overriding method that still wants to run the parent's original logic.

Summary

In this lesson, you covered extending a component with extends, calling a parent's method with super, and why ColdFusion uses interfaces instead of multiple inheritance.

What's Next?

The next lesson covers polymorphism — how different components in the same hierarchy can respond to the same method call in their own way.