Static Classes extending Abstract Classes

B

Brybot

Apparently it is not possible for a static class to extend an abstract
class? I was wondering how else I might be able to go about my problem
here?

I have a base class Parent which has a static method and an abstract
method.

public abstract class Parent {

public static bool True()
{
return true;
}

public abstract (static?) bool Foo();

}

and i want to be able to implement Foo in a static child class, but
also statically access True()

but something like this is not allowed:

public static class Child: Parent {

public override static bool Foo()
{
return false;
}
}

I just want to be able to go Child.Foo() or Child.True()?

Any help would be GREATLY appreciated!

Thanks in advance.
 
B

Bruce Wood

Brybot said:
Apparently it is not possible for a static class to extend an abstract
class? I was wondering how else I might be able to go about my problem
here?

I have a base class Parent which has a static method and an abstract
method.

public abstract class Parent {

public static bool True()
{
return true;
}

public abstract (static?) bool Foo();

}

and i want to be able to implement Foo in a static child class, but
also statically access True()

but something like this is not allowed:

public static class Child: Parent {

public override static bool Foo()
{
return false;
}
}

I just want to be able to go Child.Foo() or Child.True()?

Any help would be GREATLY appreciated!

Thanks in advance.

What exactly is your original problem (to which this is your solution)?
Perhaps there is a more conventional way to solve the original problem.
 
B

Brite

its for authenticating users over mutliple modules i am doing.

the abstract class handles reading/writing session data which is common
across all the modules, and the children classes do the
login/logout/permissions per each module.

I want the abstract base class to enforce these in the child
authentication classes and implement the common session methods.

Doesn't seem like a difficult problem but i can't seem to make it work
when everything is static?
 
D

David Browne

Brite said:
its for authenticating users over mutliple modules i am doing.

the abstract class handles reading/writing session data which is common
across all the modules, and the children classes do the
login/logout/permissions per each module.

I want the abstract base class to enforce these in the child
authentication classes and implement the common session methods.

Doesn't seem like a difficult problem but i can't seem to make it work
when everything is static?

Yes. Because static scope is not polymorphic. Just make everything
non-static and you will be fine.

You can always pin an instance to static scope using a factory method or a
singleton.

David
 
T

Tom Spink

Brite said:
its for authenticating users over mutliple modules i am doing.

the abstract class handles reading/writing session data which is common
across all the modules, and the children classes do the
login/logout/permissions per each module.

I want the abstract base class to enforce these in the child
authentication classes and implement the common session methods.

Doesn't seem like a difficult problem but i can't seem to make it work
when everything is static?

Hi,

Perhaps you would benefit from using instance methods, and implementing the
Singleton pattern?
 
B

Brite

I think we're going to drop the abstract interface, and simply extend
those Base class static methods with the child static methods... not
the nicest solution but turns out non-static is more important then
abstracting. Thanks for your help guys.
 
B

Bruce Wood

David said:
Yes. Because static scope is not polymorphic. Just make everything
non-static and you will be fine.

You can always pin an instance to static scope using a factory method or a
singleton.

I agree: drop the static thing. Use Singletons instead.

I had exactly the same problem with data access. I started out using
static and ran into the same problems you did, plus the problem that
"static" things can't be passed to methods, so I couldn't take full
advantage of polymorphism.

I switched to singletons, and the design works much, much better.

However, when I say "singletons," I do mean a kind of cheating
singleton where everything isn't locked-down private as it is in the
typical singleton, but rather protected, so that you can make an
abstract base class and derive from it, exactly as you're describing.
It's "singleton" in the sense that there can only ever be one each of
the child classes; the base class isn't really singleton itself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top