J
John Salerno
Hi all. I have a question about virtual and override methods. Please
forgive the elementary nature!
First off, let me quote Programming in the Key of C#: "Any virtual
method overridden with 'override' remains a virtual method for further
descendent classes."
Now here's my question: Let's say you have base class A, and
subclasses B and C. Class A contains a virtual method, and B contains
an override method. If C didn't have an override, does that mean it
inherits the virtual method from A? Is that what the above quote is
saying?
If so, here's another quote: "If necessary, methods or properties can
make calls to overridden members in the base class by prefacing the
method or property name with the keyword 'base.'"
In other words, if base class A contained public virtual int Method(),
and B contained public override int Method(), then for C, if I want to
make use of the method in class A, do I have to write it as:
base.Method()? If so, why would I need to do that if C already
inherits all public methods from A? Couldn't I just use Method()
without the base prefix?
The example in the book contains a base class and several subclasses.
All but one subclass overrides a virtual method in the base class, and
the other subclass (the one that has no override) uses this
'base.Method()' code to refer to the method in the base class. It
seems unnecessary, if this subclass has inherited the method already.
Thanks!
forgive the elementary nature!
First off, let me quote Programming in the Key of C#: "Any virtual
method overridden with 'override' remains a virtual method for further
descendent classes."
Now here's my question: Let's say you have base class A, and
subclasses B and C. Class A contains a virtual method, and B contains
an override method. If C didn't have an override, does that mean it
inherits the virtual method from A? Is that what the above quote is
saying?
If so, here's another quote: "If necessary, methods or properties can
make calls to overridden members in the base class by prefacing the
method or property name with the keyword 'base.'"
In other words, if base class A contained public virtual int Method(),
and B contained public override int Method(), then for C, if I want to
make use of the method in class A, do I have to write it as:
base.Method()? If so, why would I need to do that if C already
inherits all public methods from A? Couldn't I just use Method()
without the base prefix?
The example in the book contains a base class and several subclasses.
All but one subclass overrides a virtual method in the base class, and
the other subclass (the one that has no override) uses this
'base.Method()' code to refer to the method in the base class. It
seems unnecessary, if this subclass has inherited the method already.
Thanks!