7

I'm working on C# code where a static method of an abstract base class is being overridden by the class that inherits the base class. Why is this being done? I thought that only a virtual method of an abstract base class should be overridden. What purpose does it serve to make the base method static, or is it the case that what I'm encountering is a suboptimal or incorrect practice? The code is allowed by the compiler.

My understanding is that a static method allows the method to be called without the encompassing type being instantiated as an object. I've only used static methods previously in static classes, and I've viewed abstract classes as simply being inheritable classes that aren't supposed to be instantiated. In this case, I wouldn't expect a static method of the abstract class to be overrideable but made virtual, as a virtual method could still contain a default implementation, and I would think that a virtual method better hides the implementation details.

13

Overloading of static methods should not be compared to overriding of instance methods. They are fundamentally different concepts.

Overriding is when implementations of a virtual method are selected at runtime based on the instance. Overloading is when one of multiple methods with the same name are selected at compile time. Overriding is a fundamental feature of object oriented polymorphism, while overloading is more like a convenience.

On the surface, overloading static methods may look like overriding. If the compiler does not find a static method in the specified class, it looks up in the inheritance hierarchy until a matching method is found. The important difference is that the method is resolved and fixed at compile time. This is why you cannot mark static methods as virtual or override (or new) since this is not real overriding.

Overriding should conform to the LSP (Liskov substitution principle) and more generally the behavior of an overriding method should conform to the contract of the virtual method. Overloaded static methods does not have such constraints, and should really be though of as distinct methods which just happen to have the same name.

  • It's way outside the scope of this question, but maybe worth noting that some of this isn't true once the dynamic keyword comes into play. – Ben Aaronson Sep 2 '15 at 8:59
  • You know, having a static virtual function actually makes conceptual sense, though the lack of it doesn't actually matter: Just make it virtual and ignore the this-pointer. And I think C# does not allow shadowing members, which means the compiler always considers the direct members and all inherited non-overridden ones of the static type too. – Deduplicator Sep 2 '15 at 18:24
  • 1
    @Deduplicator: I'm not sure a virtual static method makes conceptual sense - since static methods are called without an instance reference, there is no way to determine which override to call. – JacquesB Sep 3 '15 at 9:07
  • @JacquesB: You need an instance, or a class-name, to determine which function to call, but not for the call itself. But as I said, they are acceptably replaced by just ignoring the implicit argument and delegating to a static function. The only cost is the additional function / indirection. – Deduplicator Sep 3 '15 at 13:29
3

In addition to @JacquesB-s excelent (+1) answer

Why override overload a static method?

I have seen usecases in java where there were static factory methods

public class Customer {
   public static Customer newInstance(String name) {} ....

public class BusinessCustomer extends Customer  {
   public static BusinessCustomer  newInstance(String name) {} ....

The reason for overloading is that both newInstance() versions have the same signature so they can be easily exchanged in sourcecode.

Customer customer1 = Customer.newInstance("peter");
Customer customer2 = BusinessCustomer.newInstance("paul");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.