Well, AMAIK, your question is essentially not about object slicing. It's more about the difference between using new
, virtual
and override
keywords.
I'll bring forward a more understandable example. Consider this code:
public class Animal
{
public virtual void Sleep()
{
Console.WriteLine(@"I'm an animal, and I want to sleep.
I just close my eyes");
}
}
public class Mammal : Animal
{
public override void Sleep()
{
Console.WriteLine(@"I'm a mammal, thus basically an animal.
Therefore, I both close my eyes, and need a warm place to sleep");
}
public bool HasBreasts
{
get
{
return true;
}
}
}
public class Human : Mammal
{
public new virtual void Sleep()
{
Console.WriteLine(@"I'm a human.
I'm so proud that I don't consider myself as an animal.
I sleep in a cozy place,
and I need a lot of money to sleep well");
}
public virtual void FallInLove();
}
public class Worker : Human
{
public override void Sleep()
{
Console.WriteLine(@"I'm a worker, and I'm under poverty line.
I have to work hard,
and I really don't know what they mean by a good sleep");
}
public override void FallInLove()
{
Console.WriteLine(@"What is love?
I need bread to survive.
I'm in the bottom-most level of Mozlow's pyramid of needs.");
}
}
Now let's analyze each keyword. Since a Worker
is basically and essentially a human, thus it HasBreasts
and also it can FallInLove()
. But wait a minute. A worker (no offense, just a funny example) has no money to fall in love. Then a worker might override
this very behavior, and forget about love.
On the other hand, all animals can sleep. They only close their eyes to sleep. However, since Mammals are hot blooded, they need a warm place. So they extend their ancestor's habit of sleeping.
But human on the other hand, defined a completely new
level of sleeping. He wants a revolution against his ancestors. He doesn't want to be known as an Animal
, so he redefines Sleep()
from scratch.
Now based on explanations of @DanPuzey, you can read rest of the story. It makes sense in the real-world.