Overriding properties from abstract class in Salesforce Apex
Asked Answered
A

1

4

I have an abstract class in apex with several properties that I would like to override in a child class. According to the documentation, properties support both the override and virtual access modifiers. However, when I try to use either of them in either the parent or child class, I get an error saying that variables cannot be marked as virtual/override. Here is a facsimile of the code that causes this error:

public abstract class Row{
    public virtual double value{
        get{return value==null ? 0 : value;}
        set;
    }
}

public class SummaryRow extends Row{
    private list<Row> childRows;
    public override double value{
        get{
            totalValue = 0;
            for(Row childRow:childRows){
                totalvalue += childRow.value;
            }
            return totalValue;
        }
    }
}

Is this functionality not supported, or is there something that I am missing?

Thanks in advance.

Apocrypha answered 15/3, 2011 at 19:9 Comment(0)
B
7

Unfortunately, as far as I know that is a mistake in the documentation. I've only been able to apply the override and virtual modifiers to methods. You can, of course, get the desired effect by manually writing your property getter/setter methods:

public abstract class TestRow {
    public Double value;

    public virtual Double getValue() {
        return value==null ? 0 : value;
    }

    public void setValue(Double value) {
        this.value = value;
    }
}

public class SummaryTestRow extends TestRow {
    private list<TestRow> childRows;

    public override Double getValue() {
        Double totalValue = 0;
        for(TestRow childRow : childRows){
            totalValue += childRow.value;
        }

        return totalValue;  
    }
}
Ballottement answered 17/3, 2011 at 19:10 Comment(2)
This is what I ended up doing, but I was hoping that there was a cleaner way and I had just messed something up with the syntax.Apocrypha
does this work with 2 way bindings in Visualforce tho? can it be bound to an input field?Maleficent

© 2022 - 2024 — McMap. All rights reserved.