473,883 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

abstract static?

What is the rationale behind the decision not to allow abstract static class
members? It doesn't seem like it's a logically contradictory concept, or
that the implementation would be difficult or near-impossible. It seems like
it would be useful. In fact, there's a place in my code that I could make
good use of it. So why not?

Chris
Nov 15 '05 #1
33 3368
If it is static, then it does not really belong to any
instance of a class. Therefore, it cannot be inherited.

Tu-Thach
-----Original Message-----
What is the rationale behind the decision not to allow abstract static classmembers? It doesn't seem like it's a logically contradictory concept, orthat the implementation would be difficult or near- impossible. It seems likeit would be useful. In fact, there's a place in my code that I could makegood use of it. So why not?

Chris
.

Nov 15 '05 #2
Chris,

The problem with this comes from the base type (abstract) not knowing
which derived type to go to for the implementation. For example, if you had
the following:

public abstract class Base
{
public static abstract void DoSomething();
}

Then you had a derived class like this:

public class Derived : Base
{
public static override void DoSomething()
{}
}

How would you indicate that you want to use the implementation in
Derived? I can have many implementations of DoSomething, and if I want to
call through the Base class, there is no way of indicating which derived
class to use.

In order to get around this now, you should use a class factory pattern
combined with a singleton pattern. The singleton pattern is used to mimic
static behavior, while the class factory pattern is used to indicate the
derived type to use (while returning the base type).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Chris Capel" <ch***@ibanktec h.comnet> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or
that the implementation would be difficult or near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make
good use of it. So why not?

Chris

Nov 15 '05 #3
I've got the exact same problem. I'd like to make sure all of my subclasses
have a certain static variable.

Look at the topic 'Static variables and inheritance'
"Chris Capel" <ch***@ibanktec h.comnet> wrote in message
news:#h******** ******@tk2msftn gp13.phx.gbl...
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or
that the implementation would be difficult or near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make
good use of it. So why not?

Chris

Nov 15 '05 #4
> The problem with this comes from the base type (abstract) not knowing
which derived type to go to for the implementation. How would you indicate that you want to use the implementation in
Derived? I can have many implementations of DoSomething, and if I want to
call through the Base class, there is no way of indicating which derived
class to use.


Fair enough. But I see having an abstract static member as more of a way to
guarantee that any non-abstract subclass will have implemented that member,
not as a way to accomplish polymorphism at all. For instance:

public abstract class ImporterBase {
protected abstract string[] ExtensionsSuppo rted { get; }
public abstract void ImportFile(stri ng path);
}

public class CsvImporter : ImporterBase {
protected override string[] ExtensionsSuppo rted {
get {
return new string[] {"csv"};
}
}
public override void ImportFile(stri ng path) {
//some stuff
}
}

Now, the only way this could really benefit a programmer, it seems, is when
reflection is used to look at the derived class. For instance:

public abstract class ImporterBase {
//stuff from above definition

public static ImporterBase GetImporter(str ing extension) {
Type[] types = Assembly.GetExe cutingAssembly( ).GetTypes(); // or
could come from other files
foreach (Type t in types) {
if (t.IsSubclassOf (typeof(IssueIm porter))) {
string[] exts =
(string[])t.GetProperty( "Extension").Ge tValue(null, null);
foreach (string e in exts)
if (e == extensionToFind )
return (ImporterBase)A ctivator.Create Instance(t);
}
}
return null;
}
}

Maybe this use-case is too uncommon and narrow to justify a language
decision like that, but it sure would be nice for me.

But then, I can just use attributes.

Chris
Nov 15 '05 #5
Correction to my code:

public abstract class ImporterBase {
protected abstract static string[] ExtensionsSuppo rted {get;}
...

public class CsvImporter : ImporterBase {
protected static override string[] ExtensionsSuppo rted {
...
"Chris Capel" <ch***@ibanktec h.comnet> wrote in message
news:ek******** *****@tk2msftng p13.phx.gbl...
The problem with this comes from the base type (abstract) not knowing which derived type to go to for the implementation.
How would you indicate that you want to use the implementation in
Derived? I can have many implementations of DoSomething, and if I want to call through the Base class, there is no way of indicating which derived
class to use.


Fair enough. But I see having an abstract static member as more of a way

to guarantee that any non-abstract subclass will have implemented that member, not as a way to accomplish polymorphism at all. For instance:

public abstract class ImporterBase {
protected abstract string[] ExtensionsSuppo rted { get; }
public abstract void ImportFile(stri ng path);
}

public class CsvImporter : ImporterBase {
protected override string[] ExtensionsSuppo rted {
get {
return new string[] {"csv"};
}
}
public override void ImportFile(stri ng path) {
//some stuff
}
}

Now, the only way this could really benefit a programmer, it seems, is when reflection is used to look at the derived class. For instance:

public abstract class ImporterBase {
//stuff from above definition

public static ImporterBase GetImporter(str ing extension) {
Type[] types = Assembly.GetExe cutingAssembly( ).GetTypes(); // or
could come from other files
foreach (Type t in types) {
if (t.IsSubclassOf (typeof(IssueIm porter))) {
string[] exts =
(string[])t.GetProperty( "Extension").Ge tValue(null, null);
foreach (string e in exts)
if (e == extensionToFind )
return (ImporterBase)A ctivator.Create Instance(t);
}
}
return null;
}
}

Maybe this use-case is too uncommon and narrow to justify a language
decision like that, but it sure would be nice for me.

But then, I can just use attributes.

Chris

Nov 15 '05 #6
More correction (::sheepish grin::)

On line 3 of ImporterBase.Ge tImporter it should read

if (t.IsSubclassOf (typeof(Importe rBase))) {

Sorry.

"Chris Capel" <ch***@ibanktec h.comnet> wrote in message
news:ek******** *****@tk2msftng p13.phx.gbl...
The problem with this comes from the base type (abstract) not knowing which derived type to go to for the implementation.
How would you indicate that you want to use the implementation in
Derived? I can have many implementations of DoSomething, and if I want to call through the Base class, there is no way of indicating which derived
class to use.


Fair enough. But I see having an abstract static member as more of a way

to guarantee that any non-abstract subclass will have implemented that member, not as a way to accomplish polymorphism at all. For instance:

public abstract class ImporterBase {
protected abstract string[] ExtensionsSuppo rted { get; }
public abstract void ImportFile(stri ng path);
}

public class CsvImporter : ImporterBase {
protected override string[] ExtensionsSuppo rted {
get {
return new string[] {"csv"};
}
}
public override void ImportFile(stri ng path) {
//some stuff
}
}

Now, the only way this could really benefit a programmer, it seems, is when reflection is used to look at the derived class. For instance:

public abstract class ImporterBase {
//stuff from above definition

public static ImporterBase GetImporter(str ing extension) {
Type[] types = Assembly.GetExe cutingAssembly( ).GetTypes(); // or
could come from other files
foreach (Type t in types) {
if (t.IsSubclassOf (typeof(IssueIm porter))) {
string[] exts =
(string[])t.GetProperty( "Extension").Ge tValue(null, null);
foreach (string e in exts)
if (e == extensionToFind )
return (ImporterBase)A ctivator.Create Instance(t);
}
}
return null;
}
}

Maybe this use-case is too uncommon and narrow to justify a language
decision like that, but it sure would be nice for me.

But then, I can just use attributes.

Chris

Nov 15 '05 #7
100
Hi Chris,

I dont see any reason to have this abstract member in the base class. You
will get the right value anyway.
Since you have the Type object to some class
(string[])t.GetProperty( "Extension").Ge tValue(null, null); will get the
property's value of this class regardless of whether you have defined that
abstract method in the base class or not.
B\rgds
100

"Chris Capel" <ch***@ibanktec h.comnet> wrote in message
news:ek******** *****@tk2msftng p13.phx.gbl...
The problem with this comes from the base type (abstract) not knowing which derived type to go to for the implementation.
How would you indicate that you want to use the implementation in
Derived? I can have many implementations of DoSomething, and if I want to call through the Base class, there is no way of indicating which derived
class to use.


Fair enough. But I see having an abstract static member as more of a way

to guarantee that any non-abstract subclass will have implemented that member, not as a way to accomplish polymorphism at all. For instance:

public abstract class ImporterBase {
protected abstract string[] ExtensionsSuppo rted { get; }
public abstract void ImportFile(stri ng path);
}

public class CsvImporter : ImporterBase {
protected override string[] ExtensionsSuppo rted {
get {
return new string[] {"csv"};
}
}
public override void ImportFile(stri ng path) {
//some stuff
}
}

Now, the only way this could really benefit a programmer, it seems, is when reflection is used to look at the derived class. For instance:

public abstract class ImporterBase {
//stuff from above definition

public static ImporterBase GetImporter(str ing extension) {
Type[] types = Assembly.GetExe cutingAssembly( ).GetTypes(); // or
could come from other files
foreach (Type t in types) {
if (t.IsSubclassOf (typeof(IssueIm porter))) {
string[] exts =
(string[])t.GetProperty( "Extension").Ge tValue(null, null);
foreach (string e in exts)
if (e == extensionToFind )
return (ImporterBase)A ctivator.Create Instance(t);
}
}
return null;
}
}

Maybe this use-case is too uncommon and narrow to justify a language
decision like that, but it sure would be nice for me.

But then, I can just use attributes.

Chris

Nov 15 '05 #8
100
Hi Nicholas,
Beside what you said I would like to add something:

You can't have polymorphism via reflection.

Consider this:

Case 1:

Type t = someObject.GetT ype();

In MSDN we can read the following for Object.GetType method:
"
.....
Return Value
The Type instance that represents the exact runtime type of the current
instance.
......
"
So polymorphism can't play any role here because we are going to call the
methods of the *actual* run-time type.

Well, we can get a type object for one of the parent classes and try to call
say
Type t = typeof(ABaseFor SomeObjectsClas s)
t.GetProperty(" PropName").GetV alue(someObject ,.....);
And to say that we really need polymorphism here. It doesn't make sence,
though. Since we have the reference to the object we always can call:
someObject.GetT ype() to get the right type object. And we are back to square
one.

Case2:

Type t = typeof(SomeType );

If we need to access instance method we need to have reference to the actual
object. You already have my *Case1*...

If we we want to use a static member.... Well, we have already specified
the type. There is no room for polymorphism again. Mainly because we don't
have an object that has a *real* type.

And to back up my point here there is some example of this.
//I assume here that *static abstract* exist
class Base
{
public abstract static void Foo();
}

class Derived1: Base
{
public override static void Foo()
{
....
}
}

class Derived2: Base
{
public override static void Foo()
{
....
}
}

Somewhere in the code

Type t = typeof(Base)

So now, if we try to call Foo via reflection, which one should be called
Derived1.Foo or Derived2.Foo?

In this case polymorphism is not possible again.

Any other way to obtain a type object belongs to one of those two cases I
believe.

The real problem is that Type objects *represent* tha given type. They are
not references to actual meta-type objects (which in fact exist internally)

B\rgds
100
"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com> wrote
in message news:ua******** ******@TK2MSFTN GP10.phx.gbl...
Chris,

In a previous thread, these reasonings were given as well. My believe
is that unless you can achieve polymorphism, then you shouldn't introduce a new language construct like this which will need additional support through another mechanism. Abstract already has a very well-defined meaning, and
that meaning would be diluted though this use of it.

If you have to resort to reflection anyways to do this, then you can
easily get around this by creating a custom attribute and applying it to the static method that you want to represent your "overridden " method in another class. Also, I think that using the design patterns pointed out represent a very clean solution to the issue as well. Having to use a language
construct in conjunction with reflection is rather dirty. If I had to use
reflection to call overridden methods on derived classes, I don't think that I would like it too much.

--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Chris Capel" <ch***@ibanktec h.comnet> wrote in message
news:ek******** *****@tk2msftng p13.phx.gbl...
The problem with this comes from the base type (abstract) not knowing which derived type to go to for the implementation.
How would you indicate that you want to use the implementation in
Derived? I can have many implementations of DoSomething, and if I want to
call through the Base class, there is no way of indicating which
derived class to use.


Fair enough. But I see having an abstract static member as more of a way

to
guarantee that any non-abstract subclass will have implemented that

member,
not as a way to accomplish polymorphism at all. For instance:

public abstract class ImporterBase {
protected abstract string[] ExtensionsSuppo rted { get; }
public abstract void ImportFile(stri ng path);
}

public class CsvImporter : ImporterBase {
protected override string[] ExtensionsSuppo rted {
get {
return new string[] {"csv"};
}
}
public override void ImportFile(stri ng path) {
//some stuff
}
}

Now, the only way this could really benefit a programmer, it seems, is

when
reflection is used to look at the derived class. For instance:

public abstract class ImporterBase {
//stuff from above definition

public static ImporterBase GetImporter(str ing extension) {
Type[] types = Assembly.GetExe cutingAssembly( ).GetTypes(); // or
could come from other files
foreach (Type t in types) {
if (t.IsSubclassOf (typeof(IssueIm porter))) {
string[] exts =
(string[])t.GetProperty( "Extension").Ge tValue(null, null);
foreach (string e in exts)
if (e == extensionToFind )
return

(ImporterBase)A ctivator.Create Instance(t); }
}
return null;
}
}

Maybe this use-case is too uncommon and narrow to justify a language
decision like that, but it sure would be nice for me.

But then, I can just use attributes.

Chris


Nov 15 '05 #9
100,

There wasn't an implication that you can have polymorphism through
Reflection, but rather, to gain the polymorphism on static members, you
would have to use a combination of reflection and the abstract keyword.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"100" <10*@100.com> wrote in message
news:u$******** **********@tk2m sftngp13.phx.gb l...
Hi Nicholas,
Beside what you said I would like to add something:

You can't have polymorphism via reflection.

Consider this:

Case 1:

Type t = someObject.GetT ype();

In MSDN we can read the following for Object.GetType method:
"
....
Return Value
The Type instance that represents the exact runtime type of the current
instance.
.....
"
So polymorphism can't play any role here because we are going to call the
methods of the *actual* run-time type.

Well, we can get a type object for one of the parent classes and try to call say
Type t = typeof(ABaseFor SomeObjectsClas s)
t.GetProperty(" PropName").GetV alue(someObject ,.....);
And to say that we really need polymorphism here. It doesn't make sence,
though. Since we have the reference to the object we always can call:
someObject.GetT ype() to get the right type object. And we are back to square one.

Case2:

Type t = typeof(SomeType );

If we need to access instance method we need to have reference to the actual object. You already have my *Case1*...

If we we want to use a static member.... Well, we have already specified
the type. There is no room for polymorphism again. Mainly because we don't
have an object that has a *real* type.

And to back up my point here there is some example of this.
//I assume here that *static abstract* exist
class Base
{
public abstract static void Foo();
}

class Derived1: Base
{
public override static void Foo()
{
....
}
}

class Derived2: Base
{
public override static void Foo()
{
....
}
}

Somewhere in the code

Type t = typeof(Base)

So now, if we try to call Foo via reflection, which one should be called
Derived1.Foo or Derived2.Foo?

In this case polymorphism is not possible again.

Any other way to obtain a type object belongs to one of those two cases I
believe.

The real problem is that Type objects *represent* tha given type. They are
not references to actual meta-type objects (which in fact exist internally)
B\rgds
100
"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com> wrote in message news:ua******** ******@TK2MSFTN GP10.phx.gbl...
Chris,

In a previous thread, these reasonings were given as well. My believe
is that unless you can achieve polymorphism, then you shouldn't introduce
a
new language construct like this which will need additional support through
another mechanism. Abstract already has a very well-defined meaning,

and that meaning would be diluted though this use of it.

If you have to resort to reflection anyways to do this, then you can
easily get around this by creating a custom attribute and applying it to

the
static method that you want to represent your "overridden " method in

another
class. Also, I think that using the design patterns pointed out represent a
very clean solution to the issue as well. Having to use a language
construct in conjunction with reflection is rather dirty. If I had to

use reflection to call overridden methods on derived classes, I don't think

that
I would like it too much.

--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Chris Capel" <ch***@ibanktec h.comnet> wrote in message
news:ek******** *****@tk2msftng p13.phx.gbl...
> The problem with this comes from the base type (abstract) not

knowing
> which derived type to go to for the implementation.

> How would you indicate that you want to use the implementation in > Derived? I can have many implementations of DoSomething, and if I want
to
> call through the Base class, there is no way of indicating which

derived > class to use.

Fair enough. But I see having an abstract static member as more of a way to
guarantee that any non-abstract subclass will have implemented that

member,
not as a way to accomplish polymorphism at all. For instance:

public abstract class ImporterBase {
protected abstract string[] ExtensionsSuppo rted { get; }
public abstract void ImportFile(stri ng path);
}

public class CsvImporter : ImporterBase {
protected override string[] ExtensionsSuppo rted {
get {
return new string[] {"csv"};
}
}
public override void ImportFile(stri ng path) {
//some stuff
}
}

Now, the only way this could really benefit a programmer, it seems, is

when
reflection is used to look at the derived class. For instance:

public abstract class ImporterBase {
//stuff from above definition

public static ImporterBase GetImporter(str ing extension) {
Type[] types = Assembly.GetExe cutingAssembly( ).GetTypes(); //
or could come from other files
foreach (Type t in types) {
if (t.IsSubclassOf (typeof(IssueIm porter))) {
string[] exts =
(string[])t.GetProperty( "Extension").Ge tValue(null, null);
foreach (string e in exts)
if (e == extensionToFind )
return

(ImporterBase)A ctivator.Create Instance(t); }
}
return null;
}
}

Maybe this use-case is too uncommon and narrow to justify a language
decision like that, but it sure would be nice for me.

But then, I can just use attributes.

Chris



Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
15384
by: Murat Tasan | last post by:
so here is another general question about java... why can't you declare an abstract static method. i can envision the case (indeed i have experienced the case) where one would want an abstracct superclass, with an abstract method such that all subclasses that implement this method make that method static. basically, the abstract class declares an abstract method that should be static for all implementations (for example a factory...
12
3104
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already ordered. I'm just too curious about this one to wait for the book. I would like to know is if it's good php programming practice to use abstract classes instead of singleton classes. For exemple a login class. I've made one as an abstract class...
3
1533
by: Sunny | last post by:
Hi again, in the past I have posted here a problem with static methods and abstract classes, and Jon Skeet and Richard Lowe have helped me to clarify the things. But now I have found another problem (I have posted similar thing in vs.ide newsgroup, but I do not know if this is and IDE or C# problem, so I post it here also).
19
24598
by: Mike Ruane-Torr | last post by:
Why can't I have a static abstract method in C#? My intention is to have a class-level method that returns a string to supply information about inherited classes, and it is natural to make this static so that I don't need an instance in order to call it. However, because my class model is using a common base class, I need to make it abstract too, so that inherited classes are forced to implement it. Am I doing something that can be...
5
3226
by: pali | last post by:
Hi, I was just wondering, why is not possible to make a member of a class BOTH abstract and static? MSDN says just that's in an error, not why this is so. In a nutshell, I need this: I have an abstract base class called EntityBase that all my other business objects are derived from. I want to be able to load and save them in a generic manner. I use a static method called EntityBase.GetByID(int id) and a member method called...
7
2419
by: Brybot | last post by:
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()
18
2963
by: Vedo | last post by:
ref struct XXX abstract sealed { literal int A = 5; }; The definition above gives me the compiler warning "C4693: a sealed abstract class cannot have any instance members 'A'". The equivalent definition is perfectly legal in other CLR languages. For example in C#; static class XXX
3
5097
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages (Java). It stores character strings, and resizes itself to accommodate new strings when needed. Interface: ----------
2
1834
by: pinki panda | last post by:
i want to knw the diff between static class and abstract class .i would appreciate if its followed by example
0
9944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9793
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11151
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10752
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9582
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5804
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4619
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.