473,662 Members | 2,593 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 3887
puzzlecracker <ir*********@gm ail.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+(BaseC lass b1, BaseClass b2)

and

public static DerivedClass operator+(Deriv edClass 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.co m>
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...@gm ail.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...@gm ail.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
3269
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 from a child class, the method uses the parent class for scope (not the originating child class). For example, see the following block of code: class Parent { private static $className = 'Parent';
7
1875
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 constructor is triggered by the first of the following events to occur within an application domain: - An instance of the class is created.
17
2347
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 oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes (this is my terminology and english is not my language so I hope you understand what I'm on about...).
4
10749
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 inherited classes are all identical. I need to refer to the static variables in each class, and each must maintain its own values for each static variable. Now: when I inherit my base classes, the static variables in all my classes contain...
11
3818
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
6
1769
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 .... CallToStaticMethod();
0
1576
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: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1681.pdf http://www.cuj.com/documents/s=8890/cujexp0310alexandr/alexandr.htm I notice the above links only talk about using policy base logic via direct inheritance and multiple inheritance.
9
5839
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 the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
2
1995
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 inherit constructors or static methods...." In the C# spec, 17.2.1 Inheritance i read the following:
0
8432
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
8344
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,...
1
8546
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4180
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...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
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.