473,414 Members | 1,606 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,414 software developers and data experts.

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 1909
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.com (Kjetil Bjerknes) wrote in message news:<1f**************************@posting.google. 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
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
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...
5
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
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...
4
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...
3
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...
17
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...
6
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...
12
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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
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
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...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.