« Home | Rainy weather, structs & classes » | Boring Tuesday » | Monday Blues, Monday Woes » | Resignation » | I've Made A Decision! » | Hmmm... » | OMG! (good version!) » | Interviews interviews.... » | Saturday Night Home Alone » | File Lister Application »

Class Inheritances (shadowing & overriding)

An example by Microsoft on derived classes, overriding, hiding virtual method (i think it's call shadowing), it just basically means you can still use the inherited function along with the base function of that class. I've added an extra line of code to show the difference for Meth2()



public class MyBase
{
public virtual string Meth1()
{
return "MyBase-Meth1";
}
public virtual string Meth2()
{
return "MyBase-Meth2";
}
public virtual string Meth3()
{
return "MyBase-Meth3";
}
}

class MyDerived : MyBase
{
// Overrides the virtual method Meth1 using the override keyword:
public override string Meth1()
{
return "MyDerived-Meth1";
}
// Explicitly hide the virtual method Meth2 using the new
// keyword:
public new string Meth2()
{
return "MyDerived-Meth2";
}
// Because no keyword is specified in the following declaration
// a warning will be issued to alert the programmer that
// the method hides the inherited member MyBase.Meth3():
public string Meth3()
{
return "MyDerived-Meth3";
}

public static void Main()
{
MyDerived mD = new MyDerived();
MyBase mB = (MyBase) mD;

System.Console.WriteLine(mB.Meth1());
System.Console.WriteLine(mB.Meth2());
System.Console.WriteLine(mD.Meth2());
System.Console.WriteLine(mB.Meth3());
System.Console.ReadLine();
}
}



Output:

MyDerived-Meth1
MyBase-Meth2
MyDerived-Meth2
MyBase-Meth3