I am not clear about why the SecondChild
class DoSomething
is not getting called again when Child
class is getting initialized.
class Parent
{
public Parent()
{
DoSomething();
}
protected virtual void DoSomething()
{
Console.WriteLine("Parent Method");
}
}
class Child : Parent
{
private string foo;
public Child()
{
foo = "HELLO";
}
protected override void DoSomething()
{
Console.WriteLine(foo.ToLower());
}
}
class SecondChild : Parent
{
public SecondChild()
{
var c = new Child();
}
protected override void DoSomething()
{
Console.WriteLine("In second Child");
}
}
class Program
{
static void Main(string[] args)
{
SecondChild c = new SecondChild();
Console.ReadLine();
}
}
I was expecting that DoSomething()
of SecondChild
will be called twice here, but instead Child
class DoSomething()
is invoked which will give NullException.