468,780 Members | 2,383 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,780 developers. It's quick & easy.

How to invoke base class static method hidden by inheritance, using derived class?

Hi buddies!

Can you tell me how to invoke base class static method hidden by inheritance, using derived class as in the following case?

using System;

class ParentClass
{
public static int i;

public static void showMethod()
{
Console.WriteLine("The value of i in ParentClass is = " + i);
}
}

class ChildClass : ParentClass
{
new static int i;

public ChildClass(int a, int b)
{
ParentClass.i = a;
i = b;
}

new public static void showMethod()
{
ParentClass.showMethod();
Console.WriteLine("The value of i in ChildClass is = " + i);
}
}

class MainClass
{
static void Main()
{
ParentClass.showMethod();

ChildClass cc = new ChildClass(1, 2);
ChildClass.showMethod();
}
}
Jul 25 '07 #1
3 1651
RoninZA
78
Remember, that because you are inheriting ChildClass from ParentClass, that ChildClass IS also a ParentClass, with extended functionality.

Watch -

Parent class:
Expand|Select|Wrap|Line Numbers
  1. public class ParentClass
  2. {
  3.     public int Counter = 0;
  4.  
  5.     public int GetCounter()
  6.     {
  7.         return Counter;
  8.     }
  9. }
  10.  
Child class:
Expand|Select|Wrap|Line Numbers
  1. public class ChildClass : ParentClass
  2. {
  3.     //public int Counter = 0;  Doesn't compile, already defined in parent
  4.     public int ChildCounter = 1;
  5.  
  6.     //public int GetCounter() Doesn't compile, already defined in parent
  7.  
  8.     public int GetChildCounter
  9.     {
  10.         return ChildCounter;
  11.     }
  12.  
  13.     public int GetParentCounter
  14.     {
  15.         return this.Counter;
  16.     }
  17. }
  18.  
And then the calling code:
Expand|Select|Wrap|Line Numbers
  1. public void main()
  2. {
  3.     ChildClass childClass = new ChildClass();
  4.     MessageBox.Show(childClass.GetCounter().ToString()); //shows 0 - ParentClass method
  5.     MessageBox.Show(childClass.GetParentCounter().ToString()); //shows 0 - ChildClass method
  6.     MessageBox.Show(childClass.GetChildCounter().ToString()); //shows 1 - ChildClass method
  7. }
  8.  
capiche? ;)
Jul 25 '07 #2
RoninZA
78
And another thing - remember that you can also cast Childclass to a ParentClass, but NOT vice versa... in other words, you can load ParentClass and ChildClass objects into a List<ParentClass>, but you can only load ChildClass objects into a List<ChildClass>.

Happy inheriting... leveraged properly, it can be extremely powerful, and save you a hell of a lot of code duplication!
Jul 25 '07 #3
Got it. Thank you very much!
Jul 27 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Andy | last post: by
5 posts views Thread by Chris Szabo | last post: by
5 posts views Thread by Dennis Jones | last post: by
47 posts views Thread by Larry Smith | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.