0

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!

Rob
  • 26,989
  • 16
  • 82
  • 98
  • Can you elaborate a bit? What do you expect it to return? Where's your code invoking this? *Hiding* methods only uses the new definition if the object you're calling it on is *cast* as the type which is doing the hiding. – Rob Jul 14 '17 at 03:59
  • I expect Jewelthief to override the Returncontents method,but it still call that method from Locksmith.After a while I was reading the book it said "Declaring your JewelThief object as a Locksmith reference causes it to call the base class ReturnContents() method and Declaring your JewelThief object as a JewelThief reference causes it to call the JewelThief's ReturnContents() method instead, because it hides the base class's method of the same name." but i could'n see any reference in this code ?so why it's still call the hiding method instead Jewelthief's method ? – Nguyễn Ngọc Hoàng Jul 14 '17 at 04:03
  • It will only 'override' the implementation if you're directly calling it on an object cast as `Jewelthief`. See linked duplicate to explain the difference – Rob Jul 14 '17 at 04:03
  • Thanks very much, finally I deeply understood it. – Nguyễn Ngọc Hoàng Jul 14 '17 at 04:38

0 Answers0