473,508 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static method inheritance

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?
Oct 6 '08 #1
3 3873
puzzlecracker <ir*********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 6 '08 #2
On Oct 6, 11:00*am, puzzlecracker <ironsel2...@gmail.comwrote:
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
Oct 6 '08 #3
Duggi wrote:
On Oct 6, 11:00 am, puzzlecracker <ironsel2...@gmail.comwrote:
>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.
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.
>

-Cnu

Oct 9 '08 #4

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

Similar topics

3
3250
by: wwight | last post by:
As many people have noticed by now, PHP exhibits some frustrating behavior when it comes to static fields and methods. For instance, when a static method is defined in a parent class, but called...
7
1867
by: Sunny | last post by:
Hi all, According C# Language Specification : 10.11 Static constructors: The static constructor for a class executes at most once in a given application domain. The execution of a static...
17
2330
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
4
10736
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
11
3795
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
6
1762
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic...
0
1565
by: Axter | last post by:
I'm currently working on the following policy base smart pointer: http://code.axter.com/smart_ptr.h Before working on the above code, I read the following links:...
9
5830
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
2
1992
by: Tim Van Wassenhove | last post by:
Hello, When i read the CLI spec, 8.10.2 Method inheritance i read the following: "A derived object type inherits all of the instance and virtual methods of its base object type. It does not...
0
7224
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,...
0
7323
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,...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
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 ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.