473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Override and "base" keyword

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.
Nov 16 '05 #1
4 1743
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?

Nov 16 '05 #2
> 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.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com... 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.

Nov 16 '05 #3
"Gecko" <na**@nada.com> a écrit dans le message de news:
u4**************@TK2MSFTNGP14.phx.gbl...
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

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #4
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.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Joseph Turian | last post by:
Fellow hackers, I have a class BuildNode that inherits from class Node. Similarly, I have a class BuildTree that inherits from class Tree. Tree includes a member variable: vector<Node>...
17
by: Krishnan | last post by:
Hi, Am having a base class that implements an interface (air code) : class BaseClass : SomeInterface { void SomeInterface.ImplMethod() { // -- code here -- } }
2
by: Wade | last post by:
Hi all, We have created some "Base" class pages for our WebForms and UserControls. For instance, when we create a WebForm called "WebForm1.aspx", instead of inheriting from "System.Web.UI.Page"...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
2
by: qazmlp1209 | last post by:
class base { public: base() { } base(int number) { priNumber = number ;
6
by: Doc John | last post by:
Can I create a "base" button that all the buttons in my Windows Forms can inherit from? Thanks. VS2005
3
by: Rob | last post by:
I have these classes (elided methods): class Base { public: Base(string name) {...} }; class Derived : public Base {
3
by: Ravi | last post by:
Is this the correct way to think of "base class"? The "base class" is a class from which other classes are derived. The "base class" will never be derived from another class.
18
by: Stephan Beal | last post by:
Hi, all! Before i ask my question, i want to clarify that my question is not about the code i will show, but about what the C Standard says should happen. A week or so ago it occurred to me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.