Override static method

0

I do not know how to ask the question, but I'll try. I have a static method in a parent class, and I planned to overwrite it in the child classes, but from what I'm seeing here, that's not possible.

I remembered a feature that has in Java, which I do not even remember how it applies to the method signature, but I know when you call the method, you call something like this:

nomeDoMetodo(NomeDaClasse.class);

I think this might be a more correct way to make the method do different behaviors.

Would you like to know if you can do this in C #?

    
asked by anonymous 04.12.2017 / 13:03

1 answer

1

I do not know what the mechanism of Java is, I will only answer the part of C #.

What you want to do is not conceptually correct and therefore has no specific mechanism to deal with this issue. Of course you can do with reflection or other mechanisms that somehow use reflection.

Static methods do not belong to the instance, they belong to the class. Only instance method can be overridden and if it is virtual.

On the other hand, I do not think it's necessary. If you want the derived class to have a static method with the same signature then create this method. You do not have to do anything else. It may have another implementation or the same implementation (although in this case it would be better to call the method it is serving as a basis, or create this method in the derived class).

Static methods must be called by the class and this by itself already prevents the polymorphism. So the need makes no sense.

    
04.12.2017 / 13:12