473,707 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"All public methods should be virtual" - yes or no / pros & cons

I'm on a team building some class libraries to be used by many other
projects.

Some members of our team insist that "All public methods should be virtual"
just in case "anything needs to be changed". This is very much against my
instincts. Can anyone offer some solid design guidelines for me?

Thanks in advance....
Nov 17 '05
164 7996
Hi Carl,

"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:O7******** ******@TK2MSFTN GP09.phx.gbl...
Frank Oquendo wrote:
Dag Henriksson wrote:
I prefer ususally 'private virtual', and I only use 'protected
virtual' if I have a good reason to.


A private method cannot be derived so what's the point of marking it
virtual?


A private method most certainly CAN be overridden in a derived class. It
simply can'y be CALLED from outside the class where it's first declared.


Not in C#. It can't be even declared as private virtual.
Or did I miss the meaning of your post?

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
Nov 17 '05 #11
Be aware that this thread is cross posted across newsgroups where
the topical languages have different sematics on virtual private methods.
In C# it's disallowed. I dont know why. The code below fails to compile with
the error: "virtual or abstract members cannot be private"

namespace CSharp
{
public class Base
{
private virtual void Test(){}
}

public abstract class Child : Base
{
private override void Test(){}
}
}

Regards /Magnus Lidbom
"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:O7******** ******@TK2MSFTN GP09.phx.gbl...
Frank Oquendo wrote:
Dag Henriksson wrote:
I prefer ususally 'private virtual', and I only use 'protected
virtual' if I have a good reason to.
A private method cannot be derived so what's the point of marking it
virtual?


A private method most certainly CAN be overridden in a derived class. It
simply can'y be CALLED from outside the class where it's first declared.

In the "private virtual" paradigm, an abstract base class exposes a
non-virtual public interface. These non-virtual functions check and

inforce the invariants of the class's interface (look up "design by contract" if
that doesn't ring a bell), and delegate to private virtual methods to
perform the "meat" of the operations. Derived classes can override the
virtual methods to tune the behavior of the class, but since only the base
class's non-virtual interface is public, everyone, including derived
classes, must go through the base interface to access the class (ensuring
that there are no holes in the invariant checking).

-cd



Nov 17 '05 #12
"Frank Oquendo" <fr****@acadxpi n.com> skrev i meddelandet
news:OH******** ******@TK2MSFTN GP10.phx.gbl...
A private method cannot be derived so what's the point of marking it
virtual?


Maybe not in C#, but it certainly can in C++ (this thread is cross-posted to
both groups).

--
Dag Henriksson
Nov 17 '05 #13
"Magnus Lidbom" <ma***********@ hotmail.com> wrote in message
news:bv******** ****@ID-204195.news.uni-berlin.de...
Be aware that this thread is cross posted across newsgroups where
the topical languages have different sematics on virtual private methods.
In C# it's disallowed. I dont know why. The code below fails to compile with the error: "virtual or abstract members cannot be private"
That is a very weird decision, and I would like to hear the rationale behind
that.

Tom.
namespace CSharp
{
public class Base
{
private virtual void Test(){}
}

public abstract class Child : Base
{
private override void Test(){}
}
}

Regards /Magnus Lidbom
"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:O7******** ******@TK2MSFTN GP09.phx.gbl...
Frank Oquendo wrote:
Dag Henriksson wrote:

> I prefer ususally 'private virtual', and I only use 'protected
> virtual' if I have a good reason to.

A private method cannot be derived so what's the point of marking it
virtual?


A private method most certainly CAN be overridden in a derived class. It simply can'y be CALLED from outside the class where it's first declared.

In the "private virtual" paradigm, an abstract base class exposes a
non-virtual public interface. These non-virtual functions check and

inforce
the invariants of the class's interface (look up "design by contract" if
that doesn't ring a bell), and delegate to private virtual methods to
perform the "meat" of the operations. Derived classes can override the
virtual methods to tune the behavior of the class, but since only the base class's non-virtual interface is public, everyone, including derived
classes, must go through the base interface to access the class (ensuring that there are no holes in the invariant checking).

-cd


Nov 17 '05 #14
Hi TT,

I guess because private is private to class and not only to outside world.
It makes sense to me..coming from C#.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"TT (Tom Tempelaere)" <_N************ **@hotmail.comM APSO_N_> wrote in
message news:da******** ***********@pho bos.telenet-ops.be...
"Magnus Lidbom" <ma***********@ hotmail.com> wrote in message
news:bv******** ****@ID-204195.news.uni-berlin.de...
Be aware that this thread is cross posted across newsgroups where
the topical languages have different sematics on virtual private methods.
In C# it's disallowed. I dont know why. The code below fails to compile with
the error: "virtual or abstract members cannot be private"


That is a very weird decision, and I would like to hear the rationale

behind that.

Tom.
namespace CSharp
{
public class Base
{
private virtual void Test(){}
}

public abstract class Child : Base
{
private override void Test(){}
}
}

Regards /Magnus Lidbom
"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam> wrote in message news:O7******** ******@TK2MSFTN GP09.phx.gbl...
Frank Oquendo wrote:
> Dag Henriksson wrote:
>
>> I prefer ususally 'private virtual', and I only use 'protected
>> virtual' if I have a good reason to.
>
> A private method cannot be derived so what's the point of marking it
> virtual?

A private method most certainly CAN be overridden in a derived class.

It simply can'y be CALLED from outside the class where it's first declared.
In the "private virtual" paradigm, an abstract base class exposes a
non-virtual public interface. These non-virtual functions check and

inforce
the invariants of the class's interface (look up "design by contract" if that doesn't ring a bell), and delegate to private virtual methods to
perform the "meat" of the operations. Derived classes can override the virtual methods to tune the behavior of the class, but since only the base class's non-virtual interface is public, everyone, including derived
classes, must go through the base interface to access the class (ensuring that there are no holes in the invariant checking).

-cd



Nov 17 '05 #15
100
Carl,
C# doesn't support private and virtual methods.

B\rgds
100

"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:O7******** ******@TK2MSFTN GP09.phx.gbl...
Frank Oquendo wrote:
Dag Henriksson wrote:
I prefer ususally 'private virtual', and I only use 'protected
virtual' if I have a good reason to.
A private method cannot be derived so what's the point of marking it
virtual?


A private method most certainly CAN be overridden in a derived class. It
simply can'y be CALLED from outside the class where it's first declared.

In the "private virtual" paradigm, an abstract base class exposes a
non-virtual public interface. These non-virtual functions check and

inforce the invariants of the class's interface (look up "design by contract" if
that doesn't ring a bell), and delegate to private virtual methods to
perform the "meat" of the operations. Derived classes can override the
virtual methods to tune the behavior of the class, but since only the base
class's non-virtual interface is public, everyone, including derived
classes, must go through the base interface to access the class (ensuring
that there are no holes in the invariant checking).

-cd

Nov 17 '05 #16
Carl Daniel [VC++ MVP] wrote:
A private method most certainly CAN be overridden in a derived class.
It simply can'y be CALLED from outside the class where it's first
declared.


Not in C#. Private makes a member accessible to the defining class alone.
Protected makes it accessible to derived classes.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 17 '05 #17
TT (Tom Tempelaere) wrote:
That is a very weird decision, and I would like to hear the rationale
behind that.


Private is just that: private to the defining type. It works the same way in
Java. It also makes the class design quite obvious as members intended for
use in derived classes are marked protected, not private. I find that level
of granularity to be quite straightforward and very useful.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 17 '05 #18
Miha Markic wrote:
A private method most certainly CAN be overridden in a derived
class. It simply can'y be CALLED from outside the class where it's
first declared.


Not in C#. It can't be even declared as private virtual.
Or did I miss the meaning of your post?


In C++. I failed to notice that this was cross-posted to the C# group as
well. More's the pitty for C# - it's a valuable idiom that they've ruled
out (Java made the same mistake).

-cd
Nov 17 '05 #19
Carl Daniel [VC++ MVP] wrote:
In C++. I failed to notice that this was cross-posted to the C#
group as well. More's the pitty for C# - it's a valuable idiom that
they've ruled out (Java made the same mistake).


I fail to see what's so terrible about it. Marking a member as protected
makes it accessible to derived classes thus giving a type's author the
ability to retain complete control over a private member.

I see the ability override private members as a problem, not a feature.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 17 '05 #20

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

Similar topics

175
8847
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
0
8697
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9288
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...
0
9157
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9058
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
8999
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
5939
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4710
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2507
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.