473,563 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OO question on overriding virtual methods

ORi
Hi all !

There's a question I've been bothering for a while:

I'm actually developing architectural frameworks for application
developing and I think virtual methods, although needed because of the
flexibility they introduce (flexibility really needed in framework
developing), are often a nuisance for final developers. They don't
like them because they never know if base class must be called and
where should they place the call if needed, so we have mostly two
different situations:

1) Base method should always be called !! : We've solved this issue by
using Template Methods, this way it doesn't matter if the class calls
base method or not.

2) Method can be overriten if needed but if it is, base method should
NOT be called. A silly example might be the following:

public class A
{
private int _att;

public virtual void foo()
{
_att = 5;
}
}

public class B:A
{
public override void foo()
{
//here they might need to alter _att's value so base.foo() should
//never be called after their code.
_att = 6;
}
}

This is specially a problem since we've changed to VS.Net 2003 and the
IDE automatically creates override sentences and inserts the base
method call which is what I want to avoid.
I can't find a way to achieve this, and any help would be appreciated
!!

thx in advance
ori
Nov 16 '05 #1
4 1921
ORi wrote:
There's a question I've been bothering for a while:

I'm actually developing architectural frameworks for application
developing and I think virtual methods, although needed because of the
flexibility they introduce (flexibility really needed in framework
developing), are often a nuisance for final developers. They don't
like them because they never know if base class must be called and
where should they place the call if needed, so we have mostly two
different situations:

1) Base method should always be called !! : We've solved this issue by
using Template Methods, this way it doesn't matter if the class calls
base method or not.

2) Method can be overriten if needed but if it is, base method should
NOT be called. A silly example might be the following:

public class A
{
private int _att;

public virtual void foo()
{
_att = 5;
}
}

public class B:A
{
public override void foo()
{
//here they might need to alter _att's value so base.foo() should
//never be called after their code.
_att = 6;
}
}

This is specially a problem since we've changed to VS.Net 2003 and the
IDE automatically creates override sentences and inserts the base
method call which is what I want to avoid.
I can't find a way to achieve this, and any help would be appreciated
!!


I'm a little confused what you're asking, because in B, _att is not
accessable, so _att=6 is not compilable.

FB
--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #2
ORi
It's a mistake, obviously _att should be protected so that could be
modified within B class.

The point is that A has a method that alters some of his attribute's
state, this method may be overriden in B to change this behaviour but
then the call to A foo() method should not be allowed

thx !
ori
public class A
{
private int _att;

public virtual void foo()
{
_att = 5;
}
}

public class B:A
{
public override void foo()
{
//here they might need to alter _att's value so base.foo() should
//never be called after their code.
_att = 6;
}
}

Nov 16 '05 #3
> I'm actually developing architectural frameworks for application
developing and I think virtual methods, although needed because of the
flexibility they introduce (flexibility really needed in framework
developing), are often a nuisance for final developers. They don't
like them because they never know if base class must be called and
where should they place the call if needed, so we have mostly two
different situations:
Im my opinion this boils down to one thing: Documentation. If you
document the methods in a way that clearly states what it does, and
what return values it has, then it should be no problem overriding the
method. If you understand how the method is supposed to work when
called directly on the base class, then why don't you understand how
it works when called from a sub-class?

I actually think it's more annoying when the developer of a base class
choose not to mark a method as virtual, because it limits severly what
I can do in my sub-class.

1) Base method should always be called !! : We've solved this issue by
using Template Methods, this way it doesn't matter if the class calls
base method or not.
I don't think it's a good idea to create a general rule like this, but
if you're unsure what the base method does (caused by bad
documentation/design :), you can't go much wrong by calling the base
method as the first instruction. Any code following the base call will
simply extend or replace the behaviour of the base method.

2) Method can be overriten if needed but if it is, base method should
NOT be called. A silly example might be the following:

public class A
{
private int _att;

public virtual void foo()
{
_att = 5;
}
}

public class B:A
{
public override void foo()
{
//here they might need to alter _att's value so base.foo() should
//never be called after their code.
_att = 6;
}
}


You should avoid declaring fields as protected (I suppose you meant to
declare _att as protected and not private or else the example will not
compile). Ensure that your classes is properly encapsulated, which
means that only methods/properties are accessible from subclasses.
</KB>
Nov 16 '05 #4
ORi
kb@trollsoft.co m (Kjetil Bjerknes) wrote in message news:<1f******* *************** ****@posting.go ogle.com>...
Im my opinion this boils down to one thing: Documentation. If you
document the methods in a way that clearly states what it does, and
what return values it has, then it should be no problem overriding the
method. If you understand how the method is supposed to work when
called directly on the base class, then why don't you understand how
it works when called from a sub-class?

I actually think it's more annoying when the developer of a base class
choose not to mark a method as virtual, because it limits severly what
I can do in my sub-class.
Yep, we already have a dozen of fantastic books documenting our
different frameworks and I'm still waiting the day when somebody will
ask me for them :) Actually we have lessons for learning how to use
the tools and everybody is supposed to be an expert with them.

In reality, life isn't so easy... While I agree that a good
documentation is very important, in large developments involving lots
of teams and different people with different skills using the same
tools you can't trust that they all will know how to use them
properly, even with all the documentation available since most of them
will never read it :)

In that kind of projects I think it's necessary to limit what they are
able to subclass or redesign in the framework, specially when talking
about frameworks, whose one of their first intentions is to unify
development process in all the company so a high degree of flexibility
in their development should be avoided.

However, I agree with you that in reduced teams, you should promote
flexibility and support it in a good documentation. I also think that
the best way to document an application is the code itself

I don't think it's a good idea to create a general rule like this, but
if you're unsure what the base method does (caused by bad
documentation/design :), you can't go much wrong by calling the base
method as the first instruction. Any code following the base call will
simply extend or replace the behaviour of the base method.
Not a general rule, the opposite. The problem is where overriding is
used in a situation where Template Method pattern should have. That is
when you have some code in a base class that has to be mandatory
executed but child classes can extend it at some point. I think it's
an error to implement it using virtual methods and expecting that the
child will call base method (even with good documentation) and should
be do it with a template. This is what I was trying to point !
You should avoid declaring fields as protected (I suppose you meant to
declare _att as protected and not private or else the example will not
compile). Ensure that your classes is properly encapsulated, which
means that only methods/properties are accessible from subclasses.


The point was not on data encapsulation or direct variable access !
Maybe the code was a bit confusing, it's intent was to ilustrate a
situation where a overriden method should not call it's base method if
overriden. I just thought a small example could help :) the discussion
on data encapsulation is beyond what I intended to expose in this
thread

Thx for all :)
ori
Nov 16 '05 #5

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

Similar topics

8
2250
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
5
2716
by: Hongzheng Wang | last post by:
Hi, I have a problem about the overriding of private methods of base class. That is, if a method f() of base class is private, can the derived class overriding f() be overriding? For example, class base {
5
2769
by: zero | last post by:
I'm having trouble with overriding methods in subclasses. I'll explain the problem using some code: class BaseClass { protected: void method2(); public: void method1();
3
4784
by: news.microsoft.com | last post by:
Hi, It is possible to override a non virtual method with the "new" keyword So how is this different from specifying a method as virtual then providing the override keyword? Is there any differences between these two methods of overriding? Thanks.
4
2210
by: Rafael Veronezi | last post by:
I have some questions about override in inheritance, and virtual members. I know that you can you override a method by two ways in C#, one, is overriding with the new keyword, like: public new bool Equals(object obj) {} Another is using the override keyword, like: public override bool Equals(object obj) {}
3
1536
by: Eric Chaves | last post by:
Hi fellows, According to the C# language specification (10.5.3), Every virtual method has a "most derived implementation" determined by a 3-step rule. If I invoke the virtual method from a normal variable, everything is ok. However if I call it inside a non-virtual method from the base class, that use's the *this*pointer to actually invoke the...
17
2899
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that when the Poodle is upcast to the Dog (in its wildest dreams) it then says "bow wow" where the bernard always says "woof" (see code). Basically, it...
6
27778
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there was anyway to force a class to call a base class's method that it is overriding? Almost the same way you have to call a base class's constructor if...
12
1534
by: danil52 | last post by:
Hello there, I have the following code: class Base { public: virtual void f() {cout << "Base::f()" << endl;} virtual void f(int) {cout << "Base::f(int)" << endl;} };
0
7659
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...
0
7580
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
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...
0
6244
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...
0
3634
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...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
916
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...

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.