In this blog i already specified about Shadowing concept . if you don't know then check it once.
- Here the following program describes the differences between shadowing and overriding what i observe are written below the program
class A { public virtual void methodsample() { Console.WriteLine("methodsample in A"); } public void methodOne() { Console.WriteLine("methodOne in A"); } public void methodTwo() { Console.WriteLine("methodTwo in A "); } public static void methodThree() { Console.WriteLine("methodThree in A"); } public static void methodFour() { Console.WriteLine("methodFour in A"); } } class B : A { public override void methodsample() { Console.WriteLine("methodsample in A overridding...This is methodsample in class B"); } public new void methodOne() { Console.WriteLine("methodOne in B "); } public new static void methodTwo() { Console.WriteLine("methodTwo in B"); } public new static void methodThree() { Console.WriteLine("methodThree in B"); } public new void methodFour() { Console.WriteLine("methodFour in B"); } } class Program { static void Main(string[] args) { A obj = new A(); obj.methodsample(); A objA = new B(); // observe in creating instance.... objA.methodsample(); objA.methodOne(); objA.methodTwo(); A.methodThree(); A.methodFour(); //objB.methodFour(); bcz static methods cannot acess with object name B objB = new B(); objB.methodOne(); B.methodTwo(); B.methodThree(); objB .methodFour(); objB.methodsample(); //objB.methodTwo(); static method ... Console.ReadLine(); } } Points I observed: /* for this program no method can be overriden . Here Just Shadowing concept will be takes place only. the flow is like : for which class you create object those methods only will be executed . Try to comment methodOne and methodFour in class B and create object for B and Execte that... for methodFour it shows an error...bcz we cannot shadow static methods... comment methodTwo in classB and observe... static methods can only call with class names .with what class name you are calling that method will be executed. A obj = new B(); is possible. B obj = new A(); Not possible. bcz A is parent class.... If you use new keyword in derived class then it doesn't show any warning means it hides the base class method and this concept is known as shadowing ... Here u observe the difference between overriding and shadowing... Observe commenting ovverride methodsample in class B and execute...*/ methodsample in A methodsample in A overridding...This is methodsample in class B methodOne in A methodTwo in A methodThree in A methodFour in A methodOne in B methodTwo in B methodThree in B methodFour in B methodsample in A overridding...This is methodsample in class B |
No comments:
Post a Comment