Does salesforce Apex support inheritance of static methods?
Asked Answered
S

1

8
public virtual class parent {
    public static void doStuff(){system.debug('stuff');} 
}

public class child extends parent{}

When I call

child.doStuff();

I get this error: Method does not exist or incorrect signature: child.doStuff()

Are static methods not inherited in salesforce or am I doing something wrong?

Saltwort answered 15/2, 2012 at 13:54 Comment(7)
Actually, according to the answer here: #5317205 virtual can only be used on methods.Seafowl
Virtual not only CAN be used on classes, but HAS to be if the class is going to be inherited from. And no, it doesn't mean every method has to be implemented. "Virtual" when applied to a method means the method CAN be overridden. And "virtual" when applied to a class means that it CAN be inherited from.Saltwort
I think the issue is not vtable related. I think this is about the public static method of the parent class not being visible using the child class moniker. In OOP theory the sample should work and the compiler should consult the parent class before rejecting method name.Syndesis
@mmix, that seems right, I think you're onto something. But why would it not be visible, have you any idea?Saltwort
well, apex is salesforce java-like language but its not Java. A lot of how it behaves is not defined by language possibility but by limitations salesforce product managers imposed artificially on it. I am sure they would have an explanation but my guess is they owuld tell you to just use parent.doStuff() :)Syndesis
Thanks. I fear you are right. Good old Apex. Thanks for you help :)Saltwort
This actually reminded me of a conversation I had with one of their PMs a while back. It had to do with controller inection pattern used in extension (versus just inheriting a controller). They are NOT big fans of OOP :) Link (look at last answer): boards.developerforce.com/t5/forums/forumtopicprintpage/…Syndesis
L
8

Apex is consistent with Java on this. Statics are not inherited.

Lanlana answered 15/2, 2012 at 14:52 Comment(9)
I was surprised to hear this, so I looked around online and I see that you are technically right - they are not "inherited" in a strict sense (no polymorphism). But I still think my example would work in Java. I don't have a Java environment to try it out in, but here is a similar example, see comments on it #1741028Saltwort
I think differences in what people assume "inherited" mena cause this confusion. Its maybe what cused the confusion with apex design as well. In broadest terms inheritance means reuse of parent, so there is nothing in OOP theory preventing child.doStuff(). I am not a Java expert, but if I remember correctly, child.doStuff(0 would work in JavaSyndesis
Yes, I realise now that it was geekbait to use the word "inheritance" in my question title! Learnt something today! Still, it seems my example would work in Java, but doesn't in Apex. Oh well.Saltwort
@naomi, static methods cannot be polymorphically inherited in ANY OOp language. Static methods are equivalent of C free functions, their address is not resolved via vtable.Syndesis
@naomi, your example would work in Java, but it's considered a bad practice. You should access statics through the class, not an instance. It also produces compiler warnings.Lanlana
@JeremyRoss, in my example I access it through the class, not an instance. I don't make an instance of Child, I'm just calling Child.doStuff(). Same as here https://mcmap.net/q/1469117/-inheritance-concept#1921822Saltwort
I see. In any case, this is one of those areas where Java and Apex differ. In Java, the compiler does compile-time method resolution for statics and it will allow you to call superclass static methods through a subclass. Apex requires you call the static on the Class in which it's defined.Lanlana
@jeremyRoss I am accepting your answer, it did not answer my question but a) I see now that my question was confusing what with the "inheritance" can o' worms, and b) you came back to me in the comments and helped me out with more info, so thanksSaltwort
this is technically wrong. Apex is not consistent with Java here because the statics can be accessed from the inheriting class in java and not in apex. Please update your answer.Batter

© 2022 - 2024 — McMap. All rights reserved.