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.
override (C# reference) An override method provides a new implementation of the method inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method. An override method must have the same signature as the overridden base method.
As per the rule, while overriding a method in subclass, parameters cannot be changed and have to be the same as in the super class.
Method overriding is a feature that allows a subclass, or a child class, to specifically implement a method already given in one of its super-classes, or parent classes, in any object-oriented programming language. Thus, the process of redefining a parent class's method in a subclass is known as method overriding.
Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile the source and hit Enter . The Employee class provides a howPaid method that will override the corresponding method in the base class.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With