Virtual-Override-Virtual

Aug 20 2009 11:31 PM

Not able to attach2 sample files, inserted the code:
It contains inheritance in the form of basecalss->derivedclass1->derivedclass2
Same function defined in all classes.

File 1: Virtual_override_override.cs
Functional is virtual in parent class while override in both classes "derivedclass1" and "derivedclass2".
It is condition that for override, virtual should be present.

When parent class "basecalss" refers to child class "derivedclass1", output is ok because virtual is present in "basecalss", override is present in "derivedclass1".
When parent class "basecalss" refers to grand child class "derivedclass2", output is confusing because this class "derivedclass2" there is override function but corresponsing virtual is not present in class "derivedclass1".
Is this behaviour ok i.e. if child is overridden and grandchild is also overridden? what's the reason.
---------------------------------------------------------------------
File 1: Virtual_virtual_override.cs
Functional is virtual in parent class and in child classe "derivedclass1" but override in  "derivedclass2".
It is condition that for override, virtual should be present.

When parent class "basecalss" refers to child class "derivedclass1", output is ok because virtual is present in "basecalss", override is not present in "derivedclass1".
When parent class "basecalss" refers to grand child class "derivedclass2", output is confusing because this class "derivedclass2" there is override function and corresponsing virtual is also present in class "derivedclass1".
Is this behaviour ok i.e. if child is not overridden but grandchild is overridden? what's the reason.
-------------------------------------------------------------
 Virtual_override_override.cs

using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ConsoleApplication1
{
class Overriding
{
}
class basecalss
{
public virtual void virtual_method()
{
Console.WriteLine("Virtual in baseclass");
}
 
}
class derivedclass1 : basecalss
{
public override void virtual_method()
{
Console.WriteLine("Override in dervied class1");
}
}
class derivedclass2 : derivedclass1
{
public override void virtual_method()
{
Console.WriteLine("Override in dervied class2");
}
}
class implement_override // UNCOMMENT THE MAIN FUNCTION
{
public static void Main()
{
basecalss baseobj;
derivedclass1 derobj1 =
new derivedclass1();
baseobj = derobj1;
baseobj.virtual_method();
derivedclass2 derobj2 =
new derivedclass2();
baseobj = derobj2;
baseobj.virtual_method();
Console.ReadLine();
 
}
}
}
/////////////////////////////////////////////////
Virtual_virtual_override.cs
//using System;
//using System.Collections.Generic;
//using System.Text;
//namespace ConsoleApplication1
//{
// class Overriding
// {
// }
// class basecalss
// {
// public virtual void virtual_method()
// {
// Console.WriteLine("Virtual in baseclass");
// }
 
// }
// class derivedclass1 : basecalss
// {
// public new virtual void virtual_method()
// {
// Console.WriteLine("Override in dervied class1");
// }
// }
// class derivedclass2 : derivedclass1
// {
// public override void virtual_method()
// {
// Console.WriteLine("Override in dervied class2");
// }
// }
// class implement_override // UNCOMMENT THE MAIN FUNCTION
// {
// public static void Main()
// {
// basecalss baseobj;
// derivedclass1 derobj1 = new derivedclass1();
// baseobj = derobj1;
// baseobj.virtual_method();
// derivedclass2 derobj2 = new derivedclass2();
// baseobj = derobj2;
// baseobj.virtual_method();
// Console.ReadLine();
 
// }
// }
//}


So main question is that when parent refers to any child class or grand child, how virtual-overrides are mapped

Answers (1)