364,033 Members | 4753 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

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

Ken Brady
P: n/a
Ken Brady
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 15 '05 #1
Share this Question
Share on Google+
175 Replies


P: n/a
If you plan on them to be derived from then yes obviously.

If its a sealed class, then its pointless and the compiler (if it doesnt)
should prevent having virtuals in a sealed class.



"Ken Brady" <Kenneth.Brady@thomson.com> wrote in message
news:O$E2E6N5DHA.1948@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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[/color]
virtual"[color=blue]
> 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....
>
>[/color]


Nov 15 '05 #2

Dag Henriksson
P: n/a
Dag Henriksson
"Ken Brady" <Kenneth.Brady@thomson.com> skrev i meddelandet
news:O$E2E6N5DHA.1948@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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[/color]
virtual"[color=blue]
> just in case "anything needs to be changed". This is very much against my
> instincts. Can anyone offer some solid design guidelines for me?[/color]

In fact, most experts think the opposite: "No public member functions should
be virtual".

A class exposes two interfaces, a calling interface (public functions) and a
derivation interface (virtual functions). They have different purpose and
different users and should be separate.

--
Dag Henriksson


Nov 15 '05 #3

Magnus Lidbom
P: n/a
Magnus Lidbom
There are very good reasons for not making a method virtual unless it needs
to be. That's why virtual is not the default in C#.
Here's Anders Hejlsberg's( the lead C# architect ) take:
http://www.artima.com/intv/nonvirtual.html

/Magnus Lidbom



"Ken Brady" <Kenneth.Brady@thomson.com> wrote in message
news:O$E2E6N5DHA.1948@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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[/color]
virtual"[color=blue]
> 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....
>
>[/color]



Nov 15 '05 #4

P: n/a
Not public no, protected only.should be virtual in my view.

Does other languages prevent PUBLIC virtuals and only allow protected
virtuals?


"Dag Henriksson" <dag_henriksson@hotmail.com> wrote in message
news:bv5ucp$onisv$1@ID-200546.news.uni-berlin.de...[color=blue]
> "Ken Brady" <Kenneth.Brady@thomson.com> skrev i meddelandet
> news:O$E2E6N5DHA.1948@TK2MSFTNGP12.phx.gbl...[color=green]
> > 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[/color]
> virtual"[color=green]
> > just in case "anything needs to be changed". This is very much against[/color][/color]
my[color=blue][color=green]
> > instincts. Can anyone offer some solid design guidelines for me?[/color]
>
> In fact, most experts think the opposite: "No public member functions[/color]
should[color=blue]
> be virtual".
>
> A class exposes two interfaces, a calling interface (public functions) and[/color]
a[color=blue]
> derivation interface (virtual functions). They have different purpose and
> different users and should be separate.
>
> --
> Dag Henriksson
>
>[/color]


Nov 15 '05 #5

n!
P: n/a
n!
> If its a sealed class, then its pointless and the compiler (if it doesnt)[color=blue]
> should prevent having virtuals in a sealed class.[/color]

FWIW the compiler will emit an error if you try to declare a new virtual
method within a sealed class (it will also give a warning about new
protected methods in a sealed class).

n!


Nov 15 '05 #6

TT \(Tom Tempelaere\)
P: n/a
TT \(Tom Tempelaere\)
"Ken Brady" <Kenneth.Brady@thomson.com> wrote in message
news:O$E2E6N5DHA.1948@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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[/color]
virtual"[color=blue]
> 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....[/color]


Mmmm.

I prefer the statement, "all virtual functions should be private", or the
milder "all virtual functions should at least be protected". That does not
include the destructor.

But that does not rule out public virtual functions.

Only functions that were designed to be virtual, should be virtual.

Tom.


Nov 15 '05 #7

Dag Henriksson
P: n/a
Dag Henriksson

<discussion@discussion.microsoft.com> skrev i meddelandet
news:OgE4wbO5DHA.2056@TK2MSFTNGP10.phx.gbl...
[color=blue]
> Not public no, protected only.should be virtual in my view.[/color]

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

--
Dag Henriksson


Nov 15 '05 #8

Frank Oquendo
P: n/a
Frank Oquendo
Dag Henriksson wrote:
[color=blue]
> I prefer ususally 'private virtual', and I only use 'protected
> virtual' if I have a good reason to.[/color]

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

--
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 15 '05 #9

Carl Daniel [VC++ MVP]
P: n/a
Carl Daniel [VC++ MVP]
Frank Oquendo wrote:[color=blue]
> Dag Henriksson wrote:
>[color=green]
>> I prefer ususally 'private virtual', and I only use 'protected
>> virtual' if I have a good reason to.[/color]
>
> A private method cannot be derived so what's the point of marking it
> virtual?[/color]

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 15 '05 #10

Miha Markic
P: n/a
Miha Markic
Hi Carl,

"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam >
wrote in message news:O70H8xO5DHA.2736@TK2MSFTNGP09.phx.gbl...[color=blue]
> Frank Oquendo wrote:[color=green]
> > Dag Henriksson wrote:
> >[color=darkred]
> >> I prefer ususally 'private virtual', and I only use 'protected
> >> virtual' if I have a good reason to.[/color]
> >
> > A private method cannot be derived so what's the point of marking it
> > virtual?[/color]
>
> 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.[/color]

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 15 '05 #11

Magnus Lidbom
P: n/a
Magnus Lidbom
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]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam >
wrote in message news:O70H8xO5DHA.2736@TK2MSFTNGP09.phx.gbl...[color=blue]
> Frank Oquendo wrote:[color=green]
> > Dag Henriksson wrote:
> >[color=darkred]
> >> I prefer ususally 'private virtual', and I only use 'protected
> >> virtual' if I have a good reason to.[/color]
> >
> > A private method cannot be derived so what's the point of marking it
> > virtual?[/color]
>
> 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[/color]
inforce[color=blue]
> 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
>
>[/color]




Nov 15 '05 #12

Dag Henriksson
P: n/a
Dag Henriksson
"Frank Oquendo" <franko@acadxpin.com> skrev i meddelandet
news:OHqVAqO5DHA.2392@TK2MSFTNGP10.phx.gbl...[color=blue]
> A private method cannot be derived so what's the point of marking it
> virtual?[/color]

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

--
Dag Henriksson


Nov 15 '05 #13

TT \(Tom Tempelaere\)
P: n/a
TT \(Tom Tempelaere\)
"Magnus Lidbom" <magnus_lidbom@hotmail.com> wrote in message
news:bv61qd$om8ug$1@ID-204195.news.uni-berlin.de...[color=blue]
> 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[/color]
with[color=blue]
> the error: "virtual or abstract members cannot be private"[/color]

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

Tom.
[color=blue]
> 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]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam >
> wrote in message news:O70H8xO5DHA.2736@TK2MSFTNGP09.phx.gbl...[color=green]
> > Frank Oquendo wrote:[color=darkred]
> > > 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?[/color]
> >
> > A private method most certainly CAN be overridden in a derived class.[/color][/color]
It[color=blue][color=green]
> > 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[/color]
> inforce[color=green]
> > 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[/color][/color]
base[color=blue][color=green]
> > class's non-virtual interface is public, everyone, including derived
> > classes, must go through the base interface to access the class[/color][/color]
(ensuring[color=blue][color=green]
> > that there are no holes in the invariant checking).
> >
> > -cd
> >
> >[/color]
>
>
>
>[/color]


Nov 15 '05 #14

Miha Markic
P: n/a
Miha Markic
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_OSPAMtiti____@hotmail.comMAPSO_N_> wrote in
message news:dawRb.7954$EZ6.309387@phobos.telenet-ops.be...[color=blue]
> "Magnus Lidbom" <magnus_lidbom@hotmail.com> wrote in message
> news:bv61qd$om8ug$1@ID-204195.news.uni-berlin.de...[color=green]
> > Be aware that this thread is cross posted across newsgroups where
> > the topical languages have different sematics on virtual private[/color][/color]
methods.[color=blue][color=green]
> > In C# it's disallowed. I dont know why. The code below fails to compile[/color]
> with[color=green]
> > the error: "virtual or abstract members cannot be private"[/color]
>
> That is a very weird decision, and I would like to hear the rationale[/color]
behind[color=blue]
> that.
>
> Tom.
>[color=green]
> > 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]"[/color][/color]
<cpdaniel_remove_this_and_nospam@mvps.org.nospam >[color=blue][color=green]
> > wrote in message news:O70H8xO5DHA.2736@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > 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.[/color][/color]
> It[color=green][color=darkred]
> > > simply can'y be CALLED from outside the class where it's first[/color][/color][/color]
declared.[color=blue][color=green][color=darkred]
> > >
> > > In the "private virtual" paradigm, an abstract base class exposes a
> > > non-virtual public interface. These non-virtual functions check and[/color]
> > inforce[color=darkred]
> > > the invariants of the class's interface (look up "design by contract"[/color][/color][/color]
if[color=blue][color=green][color=darkred]
> > > that doesn't ring a bell), and delegate to private virtual methods to
> > > perform the "meat" of the operations. Derived classes can override[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > virtual methods to tune the behavior of the class, but since only the[/color][/color]
> base[color=green][color=darkred]
> > > class's non-virtual interface is public, everyone, including derived
> > > classes, must go through the base interface to access the class[/color][/color]
> (ensuring[color=green][color=darkred]
> > > that there are no holes in the invariant checking).
> > >
> > > -cd
> > >
> > >[/color]
> >
> >
> >
> >[/color]
>
>[/color]


Nov 15 '05 #15

100
P: n/a
100
Carl,
C# doesn't support private and virtual methods.

B\rgds
100

"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam >
wrote in message news:O70H8xO5DHA.2736@TK2MSFTNGP09.phx.gbl...[color=blue]
> Frank Oquendo wrote:[color=green]
> > Dag Henriksson wrote:
> >[color=darkred]
> >> I prefer ususally 'private virtual', and I only use 'protected
> >> virtual' if I have a good reason to.[/color]
> >
> > A private method cannot be derived so what's the point of marking it
> > virtual?[/color]
>
> 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[/color]
inforce[color=blue]
> 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
>
>[/color]


Nov 15 '05 #16

Frank Oquendo
P: n/a
Frank Oquendo
Carl Daniel [VC++ MVP] wrote:
[color=blue]
> 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.[/color]

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 15 '05 #17

Frank Oquendo
P: n/a
Frank Oquendo
TT (Tom Tempelaere) wrote:
[color=blue]
> That is a very weird decision, and I would like to hear the rationale
> behind that.[/color]

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 15 '05 #18

Carl Daniel [VC++ MVP]
P: n/a
Carl Daniel [VC++ MVP]
Miha Markic wrote:[color=blue][color=green]
>> 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.[/color]
>
> Not in C#. It can't be even declared as private virtual.
> Or did I miss the meaning of your post?[/color]

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 15 '05 #19

Frank Oquendo
P: n/a
Frank Oquendo
Carl Daniel [VC++ MVP] wrote:
[color=blue]
> 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).[/color]

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 15 '05 #20

Magnus Lidbom
P: n/a
Magnus Lidbom
"Frank Oquendo" <franko@acadxpin.com> wrote in message
news:eWO3mUP5DHA.632@TK2MSFTNGP12.phx.gbl...[color=blue]
> TT (Tom Tempelaere) wrote:
>[color=green]
> > That is a very weird decision, and I would like to hear the rationale
> > behind that.[/color]
>
> Private is just that: private to the defining type. It works the same way[/color]
in[color=blue]
> Java.[/color]

In this case it's important to be clear about what it is that is private.
Certainly private should mean that no other class may access the member.
Whether it should mean that you can't override it if it's declared virtual
is another matter altogether. There are many situations where private
virtuals as implemented in C++ are appropriate. I too would like to know why
they are prohibited in C#.

/Magnus Lidbom

<snip>
[color=blue]
> --
> There are 10 kinds of people. Those who understand binary and those who
> don't.
>
> http://code.acadx.com
> (Pull the pin to reply)
>
>[/color]



Nov 15 '05 #21

Frank Oquendo
P: n/a
Frank Oquendo
Magnus Lidbom wrote:
[color=blue]
> In this case it's important to be clear about what it is that is
> private. Certainly private should mean that no other class may access
> the member. Whether it should mean that you can't override it if it's
> declared virtual is another matter altogether. There are many
> situations where private virtuals as implemented in C++ are
> appropriate. I too would like to know why they are prohibited in C#.[/color]

Because they're unnecessary thanks to the protected keyword. Why support two
ways to override a member when one access modifier has semantics the other
does not?

Considering nothing is virtual by default in C#, it makes perfect sense to
use protected virtual instead of private virtual.

--
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 15 '05 #22

n!
P: n/a
n!
> Because they're unnecessary thanks to the protected keyword. Why support
two[color=blue]
> ways to override a member when one access modifier has semantics the other
> does not?
>
> Considering nothing is virtual by default in C#, it makes perfect sense to
> use protected virtual instead of private virtual.[/color]

The point about "private virtual" is that it allows child classes to
override the method but not call it, if the same method is defined
'protected virtual' then a derived class *can* call it. Whilst it may seem
esoteric, there are times when it's useful. Though TBH I've never needed
private virtual in C# so far.

n!


Nov 15 '05 #23

P: n/a
protected means they are visible in inherited classes, wtf good is a private
declared virtual when u cant override them?

"Dag Henriksson" <dag_henriksson@hotmail.com> wrote in message
news:bv5vk1$obvqu$1@ID-200546.news.uni-berlin.de...[color=blue]
>
> <discussion@discussion.microsoft.com> skrev i meddelandet
> news:OgE4wbO5DHA.2056@TK2MSFTNGP10.phx.gbl...
>[color=green]
> > Not public no, protected only.should be virtual in my view.[/color]
>
> I prefer ususally 'private virtual', and I only use 'protected virtual' if[/color]
I[color=blue]
> have a good reason to.
>
> --
> Dag Henriksson
>
>[/color]


Nov 15 '05 #24

P: n/a
I wont be buying your product then as its gona perform like the shit with
everything virtual for no damn good reason.



"Ken Brady" <Kenneth.Brady@thomson.com> wrote in message
news:O$E2E6N5DHA.1948@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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[/color]
virtual"[color=blue]
> 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....
>
>[/color]


Nov 15 '05 #25

175 Replies

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp