static method inheritance

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
P

puzzlecracker

Are static method inheritable under csharp rules, if so how can they
be used? Can they be virtual?

They reason I am asking is because operators are static, and I don't
understand how can we apply operators to base and derive classes
simultaneously?
 
puzzlecracker said:
Are static method inheritable under csharp rules, if so how can they
be used? Can they be virtual?

No, they can't be virtual - they're not applied polymorphically.
They reason I am asking is because operators are static, and I don't
understand how can we apply operators to base and derive classes
simultaneously?

What do you mean? Operators are only ever overloaded, not overridden.
So you could write:

public static BaseClass operator+(BaseClass b1, BaseClass b2)

and

public static DerivedClass operator+(DerivedClass d1, DerivedClass d2)

That doesn't involve any overriding, just overloading. However, I would
be careful of overloading operators for classes within an inheritance
hierarchy. The lack of polymorphism could create surprising results.
 
Are static method inheritable under csharp rules, if so how can they
be used? Can they be virtual?

They reason I am asking is because operators are static, and I don't
understand how can we apply operators to base and derive classes
simultaneously?

Static Methods are available to derived classes via class name. All
the inheritance rules (public, protected, etc) apply as in normal
case. Only difference is that you need to access them via class name.

However, a static method can not be virtual or abstract. Because
static methods are class level methods not object dependent methods.
So it makes sense not to virtualize them.
They reason I am asking is because operators are static, and I don't
understand how can we apply operators to base and derive classes
simultaneously?

I think operators overloaded in base class are not much useful for the
derived class.

Consider the following code

public class A
{
public int i = 10;

public static A operator +(A aObj)
{
aObj.i++;
return aObj;
}
}

public class B : A
{

}

If I write code below

B bObj = new B();
B bObj2 = new B();
B bObj3 = bObj + bObj2;

compiler will comply that + is not defined on Class B.


-Cnu
 
Duggi said:
Static Methods are available to derived classes via class name. All
the inheritance rules (public, protected, etc) apply as in normal
case. Only difference is that you need to access them via class name.

However, a static method can not be virtual or abstract. Because
static methods are class level methods not object dependent methods.
So it makes sense not to virtualize them.


I think operators overloaded in base class are not much useful for the
derived class.

Consider the following code

public class A
{
public int i = 10;

public static A operator +(A aObj)
{
aObj.i++;
return aObj;
}
}

public class B : A
{

}

If I write code below

B bObj = new B();
B bObj2 = new B();
B bObj3 = bObj + bObj2;

compiler will comply that + is not defined on Class B.

Are you sure? I think it would complain that operator + returns an A, and
you tried to put it in a variable of type B. There's no available
conversion for that.
 
Back
Top