Skip to main content
Inspiring
March 5, 2025
Answered

Is it possible to override property types when extending a component in CF 2023?

  • March 5, 2025
  • 1 reply
  • 703 views

I am testing code that works in CF 11 for an upgrade to CF 2023. I have a component that extends another component and overrides the type of a property in the base component. Here is some simple pseudo code demonstrating the components:

 

Base.cfc:

 

component accessors="true" {
    property name="myData" type="query";
}

 

 

Extend.cfc:

 

component extends="Base" accessors="true" {
    property name="myData" type="array";

    public Extend function init() {
        setmyData([]);

        return THIS;
    }
}

 

 

When Extend.init() is called, an error is thrown saying that the value is not of type "query". This code works fine in CF 11. Has something changed since that won't allow this, or, is there a workaround?

 

Thanks,

 

Eric

    Correct answer ericbelair

    Thanks, @ericbelair . I have my doubts though. 

     

    My explanation makes sense for a strongly-typed language. But ColdFusion is weakly-typed. That hasn't changed since CF 11. Therefore, I would expect it to do type-checking at run-time. That is to say, the child component (Extend.cfc) should be able to "hide" the property MyData of the parent component (Base.cfc). In short, I now think that your code should work, just as it did in CF 11.

     

    The Adobe ColdFusion team has definitely changed the way inheritance works. But they possibly made a mistake in the manner in which properties, accessors and mutators are inherited by child components. 

     

    Hence, on second thoughts, I would suggest that you report a bug. It will get the team to take another look.  


    I have submitted a bug. We'll see what happens. In the meantime, I will try the method override option. If that doesn't work, I'll just have to create a bunch of duplicate code and no longer extend the component.

     

    Thanks,

     

    Eric

    1 reply

    BKBK
    Community Expert
    Community Expert
    March 5, 2025

    I can confirm what you have found. It happens not only in ColdFusion 2023, but also in ColdFusion 2025. It is understandable that you expect the CF 11 code to work on CF 2023. After all, new ColdFusion versions have a tradition of promising backward-compatibility. However, I think that that is an unrealistic expectation in this case.

     

    The gap between CF 11 and CF 2023 is 4 major versions. The time span is 9 years. Besides, CF 11, CF 2016 and CF 2018 have all passed their end-of-live. So, in all likelihood, Adobe won't pay close attention to these old versions when modernizing ColdFusion. 

     

    With each upgrade to a new version, the Adobe team does a lot of pruning and optimalization. As a result, recent versions of the ColdFusion application server are less weakly-typed, more accurate and more closely aligned with Java. Also, each new ColdFusion version deprecates some of the outdated software

     

    That said, the example you give here involves a fundamental functionality of ColdFusion, of the underlying Java or of any other object-oriented language for that matter. Namely, the inheritance of the fields of a superclass by a subclass. In other words, access by a subclass to a field of the superclass.

     

    With regard to this specific topic, I am not aware of any changes between CF 11 and CF 2023. What ColdFusion might have made stricter since CF 11 is the way child components inherit fields from parent components. I would expect ColdFusion to do this in line with Java. 

     

    In Java you ensure this in one of two ways:

    1.  declaring the field as "protected" in the superclass;
    2.  letting the subclass declare a variable with the same name as the one in the parent class. The variable of the parent class is then said to be "hidden" by the child class, not overridden. As far as I know, you can only override methods, not variables.


    Your example is of the second category. So let's look closer into case 2.

     

    Suppose the "hidden" field is of reference type. For example, a query, as in your case, Then, in Java, the class (type) of the field must be known at compile-time. Whereas, method-overriding and field-hiding happen at runtime.

     

    I would expect the behaviour to be the same in ColdFusion. That is, the type of the MyData property is locked in as Query at compile-time. Hence the result that you got. 

     

    A possible workaround is to use method overriding. This, you will recall, may happen at runtime. 

     

    The method to override is, of course, setMyData:

     

    /* Extend.cfc */
    component accessors="true" extends="Base" {
        public Extend function init() {   
        	variables.setMyData(queryNew(""))	
            return THIS;
        }
        public void function setMyData(q) {   
        	variables.mydata = ["123abc"];
        }
    }
    /* testPage.cfm */
    extendObject=new Extend();
    writedump(extendObject.getMyData());

     

     

     

     

     

    Inspiring
    March 5, 2025

    Thanks, I appreciate your very well explained response. I'm definitely not expecting all the code to be compatible, I think I was confused because I couldn't find this change in any of the documentation, but, your explanation makes sense. I should've thought of trying a method override. I'll give. this is a shot. 

    BKBK
    Community Expert
    Community Expert
    March 6, 2025

    Thanks, @ericbelair . I have my doubts though. 

     

    My explanation makes sense for a strongly-typed language. But ColdFusion is weakly-typed. That hasn't changed since CF 11. Therefore, I would expect it to do type-checking at run-time. That is to say, the child component (Extend.cfc) should be able to "hide" the property MyData of the parent component (Base.cfc). In short, I now think that your code should work, just as it did in CF 11.

     

    The Adobe ColdFusion team has definitely changed the way inheritance works. But they possibly made a mistake in the manner in which properties, accessors and mutators are inherited by child components. 

     

    Hence, on second thoughts, I would suggest that you report a bug. It will get the team to take another look.