Sunday, September 29, 2013

Difference between shadow and override inheritance in c#

Let's know the solution of following questions
Ques: What is difference between shadow and override inheritance in c#
Ques: Shadowing and Overriding in C# dot net
Ques: Inheritance features in C# dot net 
"Shadowing and overriding are both used when a derived class inherits from a base class, and both redefine one declared element with another. But there are significant differences between the two"
When you extend a class, you shadow fields with the same name in the base class and override virtual methods with the same name and parameter list in the base class. Overriding makes the base class method invisible. Shadowing a field only hides the field from view. You can still explicitly touch the hidden shadowed field if you wish. You can't touch an invisible overridden method.

Example
class Base
    {
        public void PrintOnScreen()
        {
            Console.WriteLine("Base Method");
        }
    }
    class Derive : Base
    {
        public new void PrintOnScreen()
        {
            Console.WriteLine("Derive Method");
        }
    }
    class MethodShadowingDemo
    {
        public static void Main(string[] args)
        {
            Base objDerive = new Derive();
            objDerive.PrintOnScreen();
            Console.ReadLine();
        }
    }

Note:
  • The calling sequence consists of the element type (FunctionSubOperator, or Property), name, parameter list, and return type. You cannot override a procedure with a property, or the other way around. You cannot override one kind of procedure (FunctionSub, or Operator) with another kind.
  • If you do not specify either Shadows or Overrides, the compiler issues a warning message to help you be sure which kind of redefinition you want to use. If you ignore the warning, the shadowing mechanism is used.
  •  If the shadowing element is inaccessible in a further derived class, shadowing is not inherited. For example, if you declare the shadowing element as Private, a class deriving from your derived class inherits the original element instead of the shadowing element.

0 comments:

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Code Imagine