Why is static not allowed on an Interface?
Could anyone tell me why 'static' is not allowed on an Interface in C#?
Satya Chauhan, Feb 02, 2011
Static classes cannot be inherited whereas an interface must be inherited in order to use it. Making an interface static, would render it pretty much useless. You'd need to inherit it in a class to implement it's members but static would prevent you from doing exactly that. Furthermore, only one instance of a static class can be held in memory upon it's creation, this would prevent you from inheriting it in multiple classes. Or even if you could, would all of your objects use the same instance? Instance of which don't have any implementation of it's members. Again, youd find your interface pretty much useless if it was static. You could say, a static class and interface are the exact opposites in many sense.
Jani Hyytiäinen, Feb 22, 2011
Static methods and variables can be accessed without the need to instantiate the class. They are initialized without object creation. As you probably know, you cannot instantiate an interface or an abstract class. When you write any methods in an interface you must implement it in the classes in which you implement that interface. But you know that static methods can be called without instantiation of the object.
Consider a scenario in which you have an interface 'ITest' with static method 'static void Execute()'. Now let class 'TestA' and 'TestB' implemented 'ITest'. You may expect to have the function definition of 'Execute()' in both classes. BUT what if you call 'ITest.Execute()', the compiler gets confused which one to call?. I think you get the idea.
Not only in C#, Java also doesn't support it and they shouldn't support it.
So, my question is, why would you need a static in interface?
Pradip Shrestha, Feb 06, 2011