I've been reading Headfirst c# and I saw a part of code that make me a bit confused about hiding method, as I understood, hiding method will hide the method which has the same name in base class, but why in this code the IDE still call the method from the baseclass? Thank you and here's the code :
class Jewels
{
public string Sparkle()
{
return "Sparkle, sparkle!";
}
}
class Safe
{
private Jewels contents = new Jewels();
private string safeCombination = "12345";
public Jewels Open(string combination)
{
if (combination == safeCombination)
return contents;
else
return null;
}
public void PickLock(Locksmith lockpicker)
{
lockpicker.WriteDownCombination(safeCombination);
}
}
class Owner
{
private Jewels returnedContents;
public void ReceiveContents(Jewels safeContents)
{
returnedContents = safeContents;
Console.WriteLine("Thank you for returning my jewels! " + safeContents.Sparkle());
}
}
class Locksmith
{
public void OpenSafe(Safe safe, Owner owner)
{
safe.PickLock(this);
Jewels safeContents = safe.Open(writtenDownCombination);
ReturnContents(safeContents, owner);
}
private string writtenDownCombination = null;
public void WriteDownCombination(string combination)
{
writtenDownCombination = combination;
}
public void ReturnContents(Jewels safeContents, Owner owner)
{
owner.ReceiveContents(safeContents);
}
}
class Jewelthief:Locksmith
{
private Jewels stolenJewels = null;
public new void ReturnContents(Jewels safeContents, Owner owner)
{//this is the place that hiding the method ReturnContents from Locksmith
stolenJewels = safeContents;
Console.WriteLine("I'm stealing the contents! " + stolenJewels.Sparkle());
}
}
Output:
Thank you for returning my jewels!Sparkle,sparkle!