Hi friends,
Good Day!
I am a beginner and started reading c# just a few days ago. We can inherit base class members/methods using override for which we must have a virutal declaration in base class. If we want to inherit from a class which we have not created what do we do? because we don`t know whether the member has been declared virtual. Also give me some real time example of inheritance where you want to override a method which may not be declared as vitual.
Thanks
Loading

Jignesh TrivediPosted May 9, 2013, 12:25 AM
yes Shadowing is concept of VB.net but we can also use it in C#.net.
Suma MPosted May 9, 2013, 12:22 AM
Jignesh TrivediPosted May 7, 2013, 12:14 AM
hi,
In this you can use Shadowing.
example
public class A {
public int ShadowTest(){ return 15;}
public virtual int InhTest(){return 15;}
}
public class B : A{
public new int ShadowTest() { return 10;} //shadow
public override int InhTest() {return 10;} //override
}
now when we call this method it retrun thire respective values.
A clA = new A();
B clB = new B();
Console.WriteLine(clA.Foo()); // output 15
Console.WriteLine(clA.Bar()); // output 15
Console.WriteLine(clB.Foo()); // output 10
Console.WriteLine(clB.Bar()); // output 10
//now let's cast B to an A class
Console.WriteLine(((A)clB).Foo()); // output 15
Console.WriteLine(((A)clB).Bar()); // output 10
hope this will help you.