Override and "base" keyword

G

Gecko

I noticed that every time I override a class member, the intellisense
behavior is to automatically add a call to the base class of the overridden
member such as:

public override void SomeMethod()
{
/// Some quality code goes here.
base.SomeMethod();
}

1) What would happen if I create an overridable member that for some reason
or another, should not be called (via the *base* keyword) from a class that
overrides it? How can I know if the method is being called from an inheritor
so I can raise an error?

2) Is the expect behavior of all overridable member be implemented so that
overriding them and calling them via the "base" keyword is completely safe?

Thank you.
 
B

Bruce Wood

1. There are some situations (particularly when programming WinForms)
in which you would create a base method that does nothing but throw a
NotImplementedException to indicate that the child class is calling the
base method and it shouldn't, or (more to the point) that the child
class didn't declare its own version (override) of the base method.

In other situations this is handled by declaring the base method
abstract so that it must be overridden but should never be called.

2. Except for the aforementioned cases, yes.

Could you give more specific information about your particular
situation?
 
G

Gecko

Could you give more specific information about your particular
situation?



Not really, I was just thinking that maybe there would be a situation when a
programmer wouldn't want the base member to be called because the code that
ran on that member was only applicable when the member was not overridden
and if that was the case what can be done about it. Just curiosity more than
anything.



Thank you.
 
J

Joanna Carter \(TeamB\)

Not really, I was just thinking that maybe there would be a situation when a
programmer wouldn't want the base member to be called because the code that
ran on that member was only applicable when the member was not overridden
and if that was the case what can be done about it. Just curiosity more than
anything.

You would not call the base method if it were marked as abstract.

Joanna
 
B

Bruce Wood

Yes, there are situations like this, when the base class is _not_
abstract (that is, can be created in its own right), and the modified
behaviour (overridden method) of the child class is completely
different and doesn't depend upon the base.

However, this _never_ applies to constructors: a child class must
_always_ call _one_of the base class constructors in order to
initialize those private members that form that part of the object's
state that is "owned" by the base class. In practical terms, if the
base class defines some private class members, something has to
initialize them, and since the child class can't see them, it _has_ to
call a base constructor to initialize them.

Apart from that, when you override a method or a property from the
parent class, you have to option of whether to call the base method /
property. Normally you want to. Sometimes you don't, depending upon
whether the functionality offered by the base method / property forms
part of the behaviour you're trying to define in the child class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top