I have an abstract parent class with some dependency and a default implementation. The constructors might look like:
public abstract class ParentClass {
protected SomeDependency someDependency;
public ParentClass(SomeDependency someDependency) {
this.someDependency = someDependency;
}
}
The dependency itself is a virtual class with methods that might be overridden, for example:
public virtual class SomeDependency {
public virtual void doSomething() {
System.debug('#### SomeDependency');
}
}
The dependency has an extension:
public class ChildDependency extends SomeDependency {
public override void doSomething() {
System.debug('#### ChildDependency ');
}
}
And the child of the original abstract parent looks like this:
public class ChildClass extends ParentClass {
public ChildClass() {
super(new ChildDependency());
}
public void doSomething() {
super.someDependency.doSomething();
}
}
In this context, I intended that "super" means that "someDependency" lives on the Parent instead of the current object.
However, when I execute this code, I get:
SomeDependency
Which tells me the code is actually interpreting this to mean the parent of ChildDependency.
If I change super.someDependency.doSomething();
to this.someDependency.doSomething();
the code works as I originally expected it to, BUT now the code is "lying" to me because someDependency is actually an instance which lives on the parent and I'd like to make that obvious in my code.
Is there a way I can make my code express this properly?
SomeDependency someDependency;
protected?#super
in methods that are designated with theoverride
keyword.". That doesn't seem to be the case here.