473,789 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1762
1. There are some situations (particularly when programming WinForms)
in which you would create a base method that does nothing but throw a
NotImplementedE xception 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*******@cana da.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. . 1. There are some situations (particularly when programming WinForms)
in which you would create a base method that does nothing but throw a
NotImplementedE xception 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.co m> 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
2104
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> nodes; // For clarity, let this be "orig_nodes" BuildTree includes a member variable:
17
2061
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
1718
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" we implement from our "Base" class page which itself inherits from "System.Web.UI.Page" -- I know, pretty standard. We do the same with our UserControls, instead they inherit from "System.Web.UI.UserControl". Now, there are some methods that we...
7
2249
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 me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the member "is a private". I believe there's more to it than just simply that... Theory: You inherit,...
2
2199
by: qazmlp1209 | last post by:
class base { public: base() { } base(int number) { priNumber = number ;
6
1567
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
1589
by: Rob | last post by:
I have these classes (elided methods): class Base { public: Base(string name) {...} }; class Derived : public Base {
3
1670
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
2462
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 that one can implement a very basic form of subclassing in C (the gurus certainly already know this, but it was news to me). What i've done (shown below) seems to work all fine and well, and does exactly what i'd expect, but i'm asking about
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.