How can a static class derive from an object?

Viewed 44418

I am trying to inherit a non-static class by a static class.

public class foo
{ }

public static class bar : foo
{ }

And I get:

Static class cannot derive from type. Static classes must derive from object.

How can I derive it from object?

The code is in C#.

16 Answers

There's no value in deriving static classes. The reasons to use inheritance are:

  • Polymorphism
  • Code reuse

You can't get polymorphism with static classes, obviously, because there is no instance to dynamically dispatch on (in other words, it's not like you can pass a Bar to a function expecting a Foo, since you don't have a Bar).

Code reuse is easily solved using composition: give Bar a static instance of Foo.

From the C# 3.0 specification, section 10.1.1.3:

A static class may not include a class-base specification (§10.1.4) and cannot explicitly specify a base class or a list of implemented interfaces. A static class implicitly inherits from type object.

In other words, you can't do this.

The error message is bogus. It's not saying "an" object. It's talking about the built-in type called "object" which is the base of everything in .NET.

It should say "static classes can not specify a base type".

Taken from http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx

The main features of a static class are:

They only contain static members.

They cannot be instantiated.

They are sealed.

They cannot contain Instance Constructors (C# Programming Guide).

So, inheriting from a non-static class violates the first feature of static classes on this list by introducing non-static members to your static class.

I don't think C# supports inheritance for static classes.

One option would be to use the singleton pattern instead

public class foo
{ }

public class bar : foo
{
    private bar instance;
    public bar GetInstance()
    {
        if(instance == null) instance = new bar();
        return instance;
    }

    private bar(){} //make default constructor private to prevent instantiation 
}

Like stated earlier the C# spec say this can't be done. You can't implement a interface with static classes either. Your best bet is to change from using a static class to using a class that uses the singleton pattern. You will have only one instance (similar to how the static class works) and you will be able to inherit behavior or implement interfaces.

You read up on Singletons here, here, and here.

Doesn't all classes (static included) already derive from object? As in, by default?

Also, like it says, "Static class cannot derive from type.", so I don't think what you are doing is possible. Why would you want a static class to derive from a type anyways?

It can't. You have to create a normal class to derive from some other class.

All classes derive implicitly from Object. That said, although static classes (which by definition are just containers for static members) "derive" from object, there is nothing you can get from that fact.

The error message is misleading.

bar can't inherit from foo because foo can be instantiated and bar can't.

If you're trying to stop people creating instances of the class, just add a private default constructor.

The OP asked "How can I drive it from object?". The object here is not an instance of another class so the answer is you don't. The object refers to the System.Object base class which is the default base class for all types in .NET. The error message is bit misleading.

Just for completeness, on the flip side, a static class also cannot be inherited or extended because it is both an abstract and a sealed class.

For everyone finding this question many years later, you might be able to do what you are hoping. There are a few scenarios where this really helps and makes sense - but it doesn't actually have anything to do with inheritance.

It is actually quite straightforward:

using static MyNamespace.MyStaticClassIWantToPretendIsABaseClass;

namespace MyNamespace
{
     static class IncludesBase
     {
          // Access static class that you are pretending is a base class
     }
}

By playing around with this, you can make your code better or worse. This can be really useful, but thinking about it as a base class is incorrect.

Related