DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2006

    Inherited method that calls new static method

    In C#, is it possible to call a new static method from an inherited method? When I try to do this, it always calls the "hidden" static method in the base class.

    eg, in the example below, when I call DoSomething() on an instance of Child, I want it to call the static method Child.DoSomethingElse() but when I implement this, it calls the static method Parent.DoSomethingElse() only. I am using the new keyword since you can't override a static method.

    public class Parent
      public static void DoSomethingElse() {  }
      public void DoSomething()
    public class Child : Parent
       public new static void DoSomethingElse() { }

  2. #2
    Join Date
    Nov 2003
    Portland, OR
    No, that's not possible because "new" breaks the inheritance chain, so DoSomething in the base class knows nothing about your new DoSomethingElse method. The only solution I can think of is to add a new DoSomething method in the Child class.
    Phil Weber

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Aug 2006
    Thanks. Regarding adding DoSomething() in the Child class, yes, that does work, although I then have to add the DoSomething() method to every derived class (I have several that inherit from Parent). I have resorted to making the DoSomethingElse() not static, using abstract in the Parent and override in the Child.

    I realize that on the surface, one would wonder why I would want the DoSomethingElse() to be static if I need to create an instance to call DoSomething() anyway. The reason is that sometimes I need to call DoSomethingElse() independently and don't want to have to create an instance to do so.

Similar Threads

  1. Understanding Use of Static Main Method
    By pacify in forum Java
    Replies: 6
    Last Post: 07-17-2006, 05:31 AM
  2. KeyListener not responding
    By lordanki in forum Java
    Replies: 3
    Last Post: 04-04-2006, 10:13 AM
  3. Is actionPerformed a static method ?
    By hugh in forum Java
    Replies: 3
    Last Post: 01-22-2006, 08:27 AM
  4. Trouble buffering transparent gif
    By axlan in forum Java
    Replies: 2
    Last Post: 06-22-2005, 08:21 AM
  5. static method question
    By Sean Woods in forum .NET
    Replies: 2
    Last Post: 02-20-2003, 05:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center