6

In .net we are not allowed to have shared function/methods in abstract classes and interfaces. Why they are not allowed ?

Is this same in other languages. like Java ?

What can be the potential problem if the Shared methods are allowed ?

Biswanath
  • 9,075
  • 12
  • 44
  • 58
  • Maybe you should clarify your question. What exactly do you mean by "shared". You can have functions (or methods that is) in abstract classes, that can be used by derived classes and other classes - that is the whole point of it. – Christian.K Dec 01 '08 at 09:13
  • 1
    @Christian.K 'Shared' is to VB what 'static' is to C# – Grokodile Apr 18 '10 at 19:58
  • @panamack: Duh! Thanks. Guess that "Public static (Shared in Visual Basic)" MSDN phrase didn't pay off for me after all ;-) – Christian.K Apr 19 '10 at 04:46

1 Answers1

12

You can certainly have static (shared) methods in abstract classes. You can't have them in interfaces, however.

It sounds like you really want virtual static/shared methods - and those aren't available. Static methods aren't called polymorphically, and with the way that most of .NET works, that wouldn't make a lot of sense. It would make sense to be able to specify static methods in interfaces when using them as type parameter constraints - an idea I've blogged about before now.

Delphi has the concept of a meta-class, where (as I understand it) instance methods in a class's meta-class are like static methods in the class itself - and one meta-class can derive from another, overriding the methods etc. I'm not a Delphi programmer, but chapter 2 of Delphi in a Nutshell might be useful to you if you want more information.

Java allows constants to be specified in interfaces, but that's the only kind of static member supported there.

Interestingly, the CLI itself does allow static methods in an interface, but that's methods with bodies - not just the signature which is provided by instance members of an interface.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • You are right about "It sounds like you really want virtual static/shared methods". – Biswanath Dec 01 '08 at 11:05
  • Delphi does that in .Net indeed by having a meta class. But since it is not supported by .Net any other .Net language can't call these methods – Lars Truijens Mar 19 '09 at 11:17
  • So, I have various classes that each work on one of my custom controls, but there is no need to instantiate an object of the class so I make all the methods in it Shared. After I got a few of them finished I noticed a pattern that I want to follow for the rest of them. So I try to extract an interface/abstract class, but I can't. You're telling me there's no way to do that in .NET? – Nick Mar 05 '14 at 14:03
  • 1
    @Nick: Yup. Polymorphism applies to instance members, not static members. Perhaps these static members should actually be instance members in a related type, which can implement an interface. – Jon Skeet Mar 05 '14 at 14:08
  • I guess I can do that, and just make it a singleton class. – Nick Mar 06 '14 at 13:26