static method inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • puzzlecracker

    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?
  • Jon Skeet [C# MVP]

    #2
    Re: static method inheritance

    puzzlecracker <ironsel2000@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 - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Duggi

      #3
      Re: static method inheritance

      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

      Comment

      • Ben Voigt [C++ MVP]

        #4
        Re: static method inheritance

        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

        Comment

        Working...