Why Can't I Inherit Static Classes

Why can't I inherit static classes?

Citation from here:

This is actually by design. There seems to be no good reason to inherit a static class. It has public static members that you can always access via the class name itself. The only reasons I have seen for inheriting static stuff have been bad ones, such as saving a couple of characters of typing.

There may be reason to consider mechanisms to bring static members directly into scope (and we will in fact consider this after the Orcas product cycle), but static class inheritance is not the way to go: It is the wrong mechanism to use, and works only for static members that happen to reside in a static class.

(Mads Torgersen, C# Language PM)

Other opinions from channel9

Inheritance in .NET works only on instance base. Static methods are defined on the type level not on the instance level. That is why overriding doesn't work with static methods/properties/events...

Static methods are only held once in memory. There is no virtual table etc. that is created for them.

If you invoke an instance method in .NET, you always give it the current instance. This is hidden by the .NET runtime, but it happens. Each instance method has as first argument a pointer (reference) to the object that the method is run on. This doesn't happen with static methods (as they are defined on type level). How should the compiler decide to select the method to invoke?

(littleguru)

And as a valuable idea, littleguru has a partial "workaround" for this issue: the Singleton pattern.

How can a static class derive from an object?

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.

inherit a static class

Static means shared and it is not inheritable. There is no way you can do this.

Inheritance with static classes

You're quite right. Reflection will show that a static class is both abstract and sealed:

  public static class MyStaticTest {
}

...

// I'm abstract
Console.WriteLine(typeof(MyStaticTest).IsAbstract ? "I'm abstract" : "");

and that's why you can't create an instance (compile time error): new MyStaticTest();. However, reflection will show that any static class is sealed as well:

 // I'm sealed 
Console.WriteLine(typeof(MyStaticTest).IsSealed ? "I'm sealed" : "");

and so you can't inherit from it (compile time error): public class MyClass: MyStaticTest {..}. Thus, all you can do with static class is to declare static members (static fields, properties, methods etc.)

Why are class static methods inherited but not interface static methods?

Here's my guess.

Since Cat can only extend one class if Cat extends Animal then Cat.identify has only one meaning. Cat can implement multiple interfaces each of which can have a static implementation. Therefore, the compiler would not know which one to choose?

However, as pointed out by the author,

Java already has this problem, with default methods. If two interfaces
declare default void identify(), which one is used? It's a compile
error, and you have to implement an overriding method (which could
just be Animal.super.identify()). So Java already resolves this
problem for default methods – why not for static methods?

If I was to guess again, I'd say that with default the implementation is part of Cat's vtable. With static it cannot be. The main function must bind to something. At compile time Cat.identify could be replaced with Animal.identify by the compiler but the code wouldn't match reality if Cat was recompiled but not the class that contains main.

Why are static classes sealed?

You can't inherit static classes because you can't override static members. You can't override static members because the entire concept of overriding members is dependent on virtual dispatch on the type of the implicit parameter, and static members have no implicit parameter to dispatch on.

Are static methods inherited in Java?

All methods that are accessible are inherited by subclasses.

From the Sun Java Tutorials:

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members

The only difference with inherited static (class) methods and inherited non-static (instance) methods is that when you write a new static method with the same signature, the old static method is just hidden, not overridden.

From the page on the difference between overriding and hiding.

The distinction between hiding and overriding has important implications. The version of the overridden method that gets invoked is the one in the subclass. The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass

Overriding or inheriting Static Class

Static members belong to the class, not the instance, and therefore are not subject to polymorphism. You cannot inherit a static class because there are no methods to inherit. And there is really no reason to inherit a static class, as you can just make a new static class and add your new methods there. If you want to have all the methods in one place, you can make wrapper methods like the ones shown below.

static class Foo {
public void Member1() { /* ... */ }
public void Member2() { /* ... */ }
public void Member3() { /* ... */ }
}

static class FooPlus {
public void Member1()
{
Foo.Member1();
}
// Repeat for all other methods
// ...

public void Member4() { /* ... */ }
}


Related Topics



Leave a reply



Submit