2

I have 2 classes

public class A
{
    public A(string N)
    {
        Name = N;
    }
    public string Name { get; set; }

    public void GetName()
    {
        Console.Write(Name);
    }
}

public class B : A
{
    public B(string N) : base(N)
    {
    }

    public new void GetName()
    {
        Console.Write(new string(Name.Reverse().ToArray()));
    }
}

I create a new object B which I want to call GetName from A and GetName from B

B foo = new B("foo");
foo.GetName(); //  output "oof"

Expected Output "foooof"

I already tried public new void GetName() : base but that does not compile

Toshi
  • 2,532
  • 4
  • 17
  • 45

3 Answers3

2

Use override and call the base class' method from the overridden method:

public class A
{
    public virtual void GetName()
    {
        Console.Write(Name);
    }
}

public class B : A
{
    public override void GetName()
    {
        base.GetName();
        Console.Write(new string(Name.Reverse().ToArray()));
    }
}

The virtual keyword is used to modify a method and allow for it to be overridden in a derived class whereas the new modifier hides the base class method. By calling the base.GetName(); you are executing the base method BTW, this is why it has no difference that you use the new or override keywords here although I recommend that use override.

References:

virtual (C# Reference)

Knowing When to Use Override and New Keywords

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • `base.GetName();` seems to work without `override` too – Toshi Oct 14 '16 at 06:27
  • 1
    @GodofSource The virtual keyword is used to modify a method and allow for it to be overridden in a derived class. So you could combine the `GetName`'s result like mentioned in the code above. – Salah Akbari Oct 14 '16 at 06:31
  • @GodofSource The override modifier extends the base class method, and the new modifier hides it. By calling `base.GetName();` you are executing the base method BTW this is why it has no difference that you use `new` or `override` here. But you should know that with `new` you hided the base method but with override you extended it. – Salah Akbari Oct 14 '16 at 06:43
  • 1
    @GodofSource `new` is dangerous. Don't use it if you don't know what you are doing. – Patrick Hofman Oct 14 '16 at 06:44
  • what means "extend"? – Toshi Oct 14 '16 at 06:46
  • @GodofSource It means the implementation of a base method can be changed by an overriding method in a derived class. – Salah Akbari Oct 14 '16 at 06:48
  • @GodofSource I included explanation in the answer. Check updated. – Salah Akbari Oct 14 '16 at 07:03
2

To get your desired output you need to call the base GetName() method in the GetName() method of the Class B. Like this for example:

public class A
{
    public A(string N)
    {
        Name = N;
    }
    public string Name { get; set; }

    public void GetName()
    {
        Console.Write(Name);
    }
}

public class B : A
{
    public B(string N) : base(N)
    {
    }

    public new void GetName()
    {
        base.GetName();
        Console.Write(new string(Name.Reverse().ToArray()));
    }
}

This will out put foooof

Bojan B
  • 2,091
  • 4
  • 18
  • 26
0
public class A
{
    public A(string N)
    {
        Name = N;
    }
    public string Name { get; set; }

    public virtual void GetName()
    {
        Console.Write(Name);
    }
}

public class B : A
{
    public B(string N) : base(N)
    {
    }

    public override void GetName()
    {
        base.GetName();
        Console.Write(new string(Name.Reverse().ToArray()));
    }
}

Use override or new according to your choice. Like to handle these scenarios.

            A foo = new B("foo");
            foo.GetName();

or

            A foo = new A("foo");
            foo.GetName();

or

            B foo = new B("foo");
            foo.GetName();

this msdn link will give you clear picture.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197