Monday, December 10, 2007

What’s a bubbled event? ::::::::::::::::
a bubbled event is the process of moving an event up the control hierarchy, from a control low in the hierarchy, such as a button in the datagrid ans prelocating it upto an ancestor control

How function overriding works in C# and what is shadowing?:::::::::::
This article explains what is Shadowing and how to achieve shadowing in C#? I mean overriding concept using override and new keywords of C#

Introduction
This article will introduce you with a very good concept of shadowing in C#.
Shadowing is a concept of polymorphism usage in Object Oriented Programming.

What is Shadowing?
Shadowing is a concept of polymorphism usage in Object Oriented Programming.This is a concept related to over-riding functions at run-time or making a shadow of the object's methods in the inherited classes.

Implementation of override to override a method
Suppose you have a base class ClassA in which you have written a method MethodA. Now, if you want to inherit this class in a ClassB and use MethodA then the MethodA declared in ClassA will be used but if you override this method in your inherited class classB then it will be over-ridden and the implementation of classB will be used whenever we use it from classB and the implementation of classA will be used whenever we use it from a instance of classA.

_#_define _TRACE
_using _System;
_using _System._Diagnostics;
_using _System._IO;

_public _class _ClassA
{
_public _virtual _void _MethodA_()
{
_Trace._WriteLine_(_"_BaseClass MethodA_"_);
}
}

_public _class _ClassB :_ClassA
{
_public _override _void _MethodA()
{
_Trace._WriteLine_(_"_SubClass MethodA overridden_"_);
}
}

//implementation class for using the above defined classes

_public _class _TopLevel
{
_static _void _Main(_string[] _args)
{
_TextWriter _tw = _Console._Out;
_Trace._Listeners._Add_(_new _TextWriterTraceListener_(_tw_)_);
_ClassA _obj = _new_ _ClassB(); \
_obj._MethodA(); // Output will be “Subclass MethodA overridden”
}
}

Shadowing instead of overriding
Upto now the code shown was for overriding a base class method with the child class method, But what will happen If you want to provide the base class behaviour instead, use the new directive, with or without virtual at the base class Is this possible in C#? Yes, it is and that is the concept of shadowing in C# using a keyword "new' which is a modifier used instead of "override".

Check out in the code below
_public _class _ClassA
{ _public _virtual _void _MethodA()
{ _Trace._WriteLine_(_"_BaseClass MethodA"_)_;
}
}

_public _class _ClassB :_ClassA
{
_public _new _void _MethodA()
{
_Trace._WriteLine_(_"_SubClass MethodA overridden_"_);
}
}

//implementation class for using the above defined classes

_public _class _TopLevel
{
_static _void _Main_(_string[] _args)
{
_TextWriter _tw = _Console._Out;
_Trace._Listeners._Add_(_new _TextWriterTraceListener_(_tw_)_);
_ClassA _obj = _new _ClassB_();
_obj._MethodA_(); // Outputs “Class A Method"
_ClassB _obj1 = _new _ClassB();
_obj._MethodA_(); // Outputs “SubClass ClassB Method”
}
}

No comments: