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?

naomi
  • 1,934
  • 1
  • 14
  • 29
  • Actually, according to the answer here: http://stackoverflow.com/questions/5316705/overriding-properties-from-abstract-class-in-salesforce-apex `virtual` can only be used on methods. – Matt Lacey Feb 15 '12 at 14:13
  • 4
    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. – naomi Feb 15 '12 at 14:26
  • 1
    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. – mmix Feb 15 '12 at 14:30
  • @mmix, that seems right, I think you're onto something. But why would it not be visible, have you any idea? – naomi Feb 15 '12 at 14:32
  • 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() :) – mmix Feb 15 '12 at 14:38
  • Thanks. I fear you are right. Good old Apex. Thanks for you help :) – naomi Feb 15 '12 at 14:46
  • 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): http://boards.developerforce.com/t5/forums/forumtopicprintpage/board-id/Visualforce/message-id/9410/print-single-message/false/page/1 – mmix Feb 15 '12 at 15:47

1 Answers1

8

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

Jeremy Ross
  • 11,544
  • 3
  • 36
  • 36
  • 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 http://stackoverflow.com/questions/1740528/inheritance-vs-static-in-java#1740662 – naomi Feb 15 '12 at 15:21
  • 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 Java – mmix Feb 15 '12 at 15:25
  • 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. – naomi Feb 15 '12 at 15:29
  • 1
    @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. – mmix Feb 15 '12 at 15:30
  • 2
    @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. – Jeremy Ross Feb 15 '12 at 15:40
  • @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 http://stackoverflow.com/questions/1921802/inheritance-concept#1921822 – naomi Feb 15 '12 at 15:44
  • 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. – Jeremy Ross Feb 15 '12 at 15:58
  • @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 thanks – naomi Feb 15 '12 at 16:09
  • 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. – NSjonas Mar 09 '16 at 19:58