-8

I have a base and sub class such as:

class BaseClass
{
    public void MethodA()
    {
        MethodB();
    }

    public void MethodB()
    {
        Debug.Log("BaseClass MethodB");
    }
}

class SubClass : BaseClass
{
    public new void MethodB() // <- without `new` keyword there's a warning on this line
    {
        Debug.Log("SubClass MethodB");
        base.MethodB();
    }
}

When the MethodA of the BaseClass instance is called it calls MethodB but only of the BaseClass, and not of the SubClass first. e.g.

var subclass = new SubClass();
subclass.MethodA(); // Does not log "SubClass MethodB" first.  Only logs "BaseClass MethodB"

How do you make sure the parent methods call the subclass methods?

Note, without the new keyword on the MethodB line, Visual Studio gives a warning: 'SubClass.MethodB()' hides inherited member 'BaseClass.MethodB()'. Use the new keyword if hiding was intended.

AJP
  • 26,547
  • 23
  • 88
  • 127
  • Note a similar question is asked here (https://stackoverflow.com/a/50184616/539490). But it's different because that question is about when you can not modify the base class. I hope this question and answer are significantly different / more concise. If you disagree then please vote to close. – AJP Jan 08 '20 at 13:05
  • Pretty similar to https://stackoverflow.com/questions/17717570/why-does-calling-a-method-in-my-derived-class-call-the-base-class-method. Actually there are hundreds of similar ones. This question is asked slightly differently once a week I guess. – MakePeaceGreatAgain Jan 08 '20 at 13:08
  • Yes @PavelAnikhouski that's the kind of answer I was looking for. Thanks! Shall we vote to close? – AJP Jan 08 '20 at 15:26
  • @AJP you can vote to close or delete this question – Pavel Anikhouski Jan 08 '20 at 15:28
  • I can't delete it, it won't let me. Have voted to close instead. Thanks. – AJP Jan 08 '20 at 15:32
  • @AJP You can delete the question if you delete the answer first. (Both is entirely up to you.) – Baum mit Augen Jan 08 '20 at 15:46

1 Answers1

1

To get Subclass.MethodB to be called from your base class you need to use the virtual and override keywords. For example:

class BaseClass
{
    public void MethodA() // invoking this will correctly log "SubClass MethodB" followed by "BaseClass MethodB"
    {
        MethodB();
    }

    public virtual void MethodB()
    {
        Debug.Log("BaseClass MethodB");
    }
}

class SubClass : BaseClass
{
    public override void MethodB()
    {
        Debug.Log("SubClass MethodB");
        base.MethodB();
    }
}
AJP
  • 26,547
  • 23
  • 88
  • 127
  • 3
    Why have you answered your own question? – Johnathan Barclay Jan 08 '20 at 13:08
  • Yeah, seems fishy! – Viggo Lundén Jan 08 '20 at 13:18
  • 3
    It's perfectly allowed to post questions and answering them yourself for the sake of documenting... though in this case it's really _really_ basic c# language functionality, so I'm not sure if it's useful at all. "How to override" is like, the first lesson in subclassing in c#. – Nyerguds Jan 08 '20 at 13:19
  • @Nyerguds yes, but why ask a question then answer it within the same minute? – Johnathan Barclay Jan 08 '20 at 13:20
  • 1
    As I said. "for the sake of documenting". This is posted here not because OP had a problem and found the solution, but specifically to put a solution to this problem on SO. And yes, that's allowed. – Nyerguds Jan 08 '20 at 13:21
  • @Nyerguds ok fair enough, but this is clearly an attempt to gain XP. – Johnathan Barclay Jan 08 '20 at 13:23
  • 1
    So? You don't _get_ xp for answering your own question. And if it helps other people out and they upvote it, then that XP is deserved. Again, according to the rules, this is a legitimate way of getting XP. Also, I don't think someone with 17k XP will really bother doing things as "an attempt to gain XP". – Nyerguds Jan 08 '20 at 13:25
  • 3
    @Nyerguds yes, I see your point, but as you noted earlier, this is very basic stuff, so it just seemed strange. – Johnathan Barclay Jan 08 '20 at 13:32
  • @Nyerguds I'm new to StackOverflow but isn't there a way to create a question for the sake of documentation where you answer it directly, instead of creating a "normal" question ? Which would have made this post more legitimate. – Philippe B. Jan 08 '20 at 14:11
  • @PhilippeB. that's what I tried to do. I'd never seen it before but there's a little check box saying "Answer your own question" ... which an hour earlier I couldn't click but after experimenting and finding the (to this very basic C# answer) I could then answer my own question :) – AJP Jan 08 '20 at 15:29
  • @Jonathan Barclay unfortunately it costed me an hour in frustration and searching for "c# call subclass method from parent" didn't give anything useful. I thought this would be a useful answer to someone but appears not. Please vote to close. I can't delete this now as too many comments. – AJP Jan 08 '20 at 15:34
  • @Nyerguds yeah I should do those beginner lessons. I haven't. The `new` word led me off down the wrong path. It was surprising that Visual Studio only gave one rather than two valid suggestions (the other missing suggestion being `virtual / override`). – AJP Jan 08 '20 at 15:36