Wednesday, December 8, 2010

C# - Static keyword & Inheritance:


     There is lot of confusion when working with static class or method in inheritance scenario we will try to settle down few by probing with actual code. So let’s start by wearing our thinking cap.
Consider Following Code Listing 1.1:
    Here we are defining two classes BaseClass also DerivedClass that inherit from BaseClass.Our BaseClass contain a static method AddTwo which takes two ints and return there sum as int.
   If you try to add one more AddTwo method which have same signature as earlier discussed static method but just non-static, compiler will raise an error ,so we cant have two method in a class with same signature except one is static other non-static.
  Our Derived class DerivedClass inherit static method of it’s base class
So if we doesn’t add a method that hide base class static method AddTwo(int,int) ,then one can access it using inheritance as
 DerivedClass.AddTwo(int, int)
     Now as the base class method AddTwo(int,int) is inherited in derived class DerivedClass we can hide it using new keyword.as follows
        public static int AddTwo(int x, int y)
        {
            return x + y;
        }

Code Listing 1.1 (below):
public class BaseClass
    {
        /*static method can'not be virtual*/
        public static int AddTwo(int x, int y)
        {
            return x + y;
        }

        /*One can not have two method with same signature one static other non-static */
        public  double AddTwo(double x, double y)
        {
            return x + y;
        }
    }
    public class DerivedClass:BaseClass
    {
        /*Static method of base class get inherited in derived class*/
        /*To hide base class implementation we use new keyword*/
        public static new int AddTwo(int x, int y)
        {
            return base.
        }
        public double Substract(double x, double y)
        {
            return x - y;
        }
    }

Further Lets Find Out If static class can be derived or not
Consider Code Listing 1.2

Code Listing 1.2 (below):
public static class StaticBaseClass
    {
        public static int AddTwo(int x, int y)
        {
            return x + y;
        }
        public static int MultiplyTwo(int x, int y)
        {
            return x * y;
        }
    }
    public static class StaticDerivedClass : StaticBaseClass
    {
     
    }
If we try to compile  code listing 1.2 compiler throws an error saying one all static class must be inherited from system.Object.So here can conclude that no static class can derive from other static class
Consider Code Listing 1.3 In this listing we are making our  NonStaticDerivedClass class non static making it inherit from a static class,again compiler raises an error saying one can’t derived from static class.So Conclusion is no non-static class can derive from static class.
Code Listing 1.3 (below):
    public static class StaticBaseClass
    {
        public static int AddTwo(int x, int y)
        {
            return x + y;
        }
        public static int MultiplyTwo(int x, int y)
        {
            return x * y;
        }
    }
    public  class NonStaticDerivedClass: StaticBaseClass
    {
     
    }

Consider Code Listing 1.4
Code Listing 1.4 (below):
   public class BaseClass
    {
        /*static method can'not be virtual*/
        public static int AddTwo(int x, int y)
        {
            return x + y;
        }
        public static int MultiplyTwo(int x, int y)
        {
            return x * y;
        }
        /*One can not have two method with same signature one static other non-static */
        public  double AddTwo(double x, double y)
        {
            return x + y;
        }
    }
    public class DerivedClass:BaseClass
    {
        /*Static method of base class get inherited in derived class*/
        /*To hide base class implementation we use new keyword*/
        public static new int AddTwo(int x, int y)
        {
            return x + y;
        }
        public double Substract(double x, double y)
        {
            return x - y;
        }
    }

    public static class StaticBaseClass
    {
        public static int AddTwo(int x, int y)
        {
            return x + y;
        }
        public static int MultiplyTwo(int x, int y)
        {
            return x * y;
        }
    }
    public static class StaticDerivedClass : BaseClass
    {
     
    }
Here in this listing we are trying to create a static class ‘StaticDerivedClass’ that inherit from Nonstatic class ‘BaseClass
This time as expected compiler raise can error saying Static class can only inherit from system.object,conclusion that no static class can inherit from non-static base class.

Now Final Conclusions:
After experimentaing for while we can draw some logical conclusion that confirm the theory  as follows
a)     Every Static class is implicitly sealed
b)    No Static class can inherit from any other class other than system.object
c)     Static method can’t be virtual method in any case
d)    Static method of non-static class get inherited into corresponding non-static derived class
e)     Static method inherited from non-static class into a another non-static class can be hidden in derived class by using new keyword.
f)     No class can have two method with same signature but one method static & other non-static

No comments:

Post a Comment