473,545 Members | 289 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 7885
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
8688
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
7465
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
7398
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
7656
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. ...
0
7752
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...
0
5969
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
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
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
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
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.