-3

Pretty new guy here, starting to look deeper onto C#.

I was wondering if you can "update" an inherited method. Here in my example the "Mage" class inherits from "Hero". Both have a "Shout" method, yet the Mage shout should add a line of text to the screen, but I only get the Hero's one.

I don't want to override Hero's Shout, but "update it" so Hero's childrens can shout something else. I was expecting new to let me modify the old methods while still using it, but no luck. What am I missing?

public class Hero
{
    protected string _name;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public Hero(string n)
    {
        Name = n;
    }

    public virtual void Shout()
    {
        Console.WriteLine("I am {0} and I am a hero! ", Name);
    }
}

public class Mage : Hero
{
    public Mage(string n) : base(n)
    {
        Name = n;
    }

    public new void Shout()
    {
        base.Shout();
        // Next line does not print...
        Console.WriteLine("Also, I am a fierce Mage!");
    }
}

Tanks for any help, tip,...!

Main could be:

 class Program
{
    static void Main(string[] args)
    {
        var h = new Hero("Paul One-Eye");
        // Old line, not working
        // var m = new Hero("Zangdar the Just");
        var m = new Mage("Zangdar the Just");

        h.Shout();
        m.Shout();
    }
}

Expected output should be :

I am Paul One-Eye and I am a hero!

I am Zangdar the Just and I am a hero!

Also, I am a fierce Mage!

EDIT: Overriding the method like this DOES change SOMETHING:

    public override void Shout()
    {
        base.Shout();
        Console.WriteLine("I am a fierce Mage!");
    }
garfbradaz
  • 3,424
  • 7
  • 43
  • 70

3 Answers3

5

Your code is working fine But you initialized your Mage with the Base Hero class

You did

var h = new Hero("Paul One-Eye");
var m = new Hero("Zangdar the Just");

It should be

var h = new Hero("Paul One-Eye");
var m = new Mage("Zangdar the Just");
Pepernoot
  • 3,409
  • 3
  • 21
  • 46
1

Just wondering why won't you override the base class method and extend it?

public class Mage : Hero
{
    public Mage(string n) : base(n)
    {
        Name = n;
    }

    public override void Shout()
    {
        base.Shout();
        Console.WriteLine("Also, I am a fierce Mage!");
    }
}

"new" modifier is used to hide the base class method while "override" modifier is used to extend the base class method (and this is exactly what you want), more info here

YuvShap
  • 3,825
  • 2
  • 10
  • 24
0

Consider this example

Your main is this:

 class Program {

    static void Main(string[] args) {
        var h = new Hero("Paul One-Eye");
        var m = new Mage("Zangdar the Just");
        Hero m2 = (Hero)new Mage("Copyright the Infringed");

        h.Shout();
        m.Shout(); 
        m2.Shout();
    }
}


public class Mage : Hero {
    public new void Shout() {
        base.Shout();  Console.WriteLine("Also, I am a fierce Mage!");
    }
}

Prints:

I am Paul One-Eye and I am a hero!

I am Zangdar the Just and I am a hero! Also, I am a fierce Mage!

I am Copyright the Infringed and I am a hero!

However if you do this:

public class Mage : Hero {
    public override void Shout() {
        base.Shout();  Console.WriteLine("Also, I am a fierce Mage!");
    }
}

Prints:

I am Paul One-Eye and I am a hero!

I am Zangdar the Just and I am a hero! Also, I am a fierce Mage!

I am Copyright the Infringed and I am a hero! Also, I am a fierce Mage!

Take your pick. If you want the hero shout to always be a hero shout even when the hero can potentially be a mage, use new. If you want the hero shout to always reflect what subtype the hero is use override

Community
  • 1
  • 1
apokryfos
  • 38,771
  • 9
  • 70
  • 114