Wednesday, April 13, 2011

How to override a Base Class method and add a parameter

I have this in my base class:

protected abstract XmlDocument CreateRequestXML();

I try to override this in my derived class:

protected override XmlDocument CreateRequestXML(int somevalue)
{
    ...rest of code
}

I know this is an easy question but because I've introduced a param is that the issue? The thing is, some of the derived classes may or may not have params in its implementation.

From stackoverflow
  • Yes the parameter is the issue, the method signature must match exactly (that includes return value and parameters).

  • When you override a method, with the exception of the word override in place of virtual or abstract, it must have the exact same signature as the original method.

    What you've done here is create a new unrelated method. It is not possible to introduce a parameter and still override.

    : Ok, so then lets say you want to make sure every class the inherits this base class to be required to implement that method. But you know that in some that there will be some incoming required params to that method and some not. Then what? Do you not require params and get them via a priv field?
    : So, you want subclasses to implement the protected method & in some subclasses you want to require params sometimes because the as relies on them and in other cases it may not. If the method is not meant to be consumed publically you could get from private members of the class...is that typical?
    : I'm just trying to figure out (best practice) when best to leave a protected or even private method parameterless & just getting what it needs from private fields instead
  • If some of the derived classes need paramters and some do not, you need to have two overloaded versions of the method. Then each derived class can override the one it needs.

    class Foo {    
        protected abstract XmlDocument CreateRequestXML(int somevalue);
        protected abstract XmlDocument CreateRequestXML();
    };
    
    class Bar : public Foo {    
        protected override XmlDocument CreateRequestXML(int somevalue) { ... };
        protected override XmlDocument CreateRequestXML()
            { CreateRequestXML(defaultInt); }
    };
    

    This of course introduces a problem of the user calling the wrong one. If there is no acceptable default for the extra paramter, you may have to throw an exception in the derived class if the wrong version is called.

  • I am very curious what you are trying to accomplish with this code.

    Normally you define an abstract function when a usage class knows its signature. The usage class has a "contract" with you base class.

    When you want to derive from the baseclass and add additional parameters, how does the usage class know what to call on the base class anyway?

    It looks like an architectual flaw....but maybe you can add some extra information to the inital topic.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.