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

Method from interface is virtual or not?

I have next interface:
interface I
{
void F();
}

And next class implements this interface:
class MyClass : I
{
void F(){};
}

What does the modifier have the method MyClass::F() by
default?
Nov 22 '05 #1
9 1404
Public. Interface implementation members are always public, and you can't
put a visibility modifier in front of them, so you can't make them anything
but public.

--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

"Vladimir Bezugliy" <vb**@inbox.ru> wrote in message
news:db****************************@phx.gbl...
I have next interface:
interface I
{
void F();
}

And next class implements this interface:
class MyClass : I
{
void F(){};
}

What does the modifier have the method MyClass::F() by
default?

Nov 22 '05 #2
>> What does the modifier have the method MyClass::F() by
default?Public. Interface implementation members are always

public, and you can'tput a visibility modifier in front of them, so you can't make them anythingbut public.


I am sorry, but you do not understand me right.
What does the modifier of the method from an interface -
"virtual" or "virtual sealed"?
As far as I see by ILDASM - method called as virtual
(callvirt).
But VisualStudio do not add any modifier in front of this
method.
And I can not override this method in derived classes.
I should explicitly add word "virtual" in front of the
method.
Why?
Nov 22 '05 #3
>> What does the modifier have the method MyClass::F() by
default?Public. Interface implementation members are always

public, and you can'tput a visibility modifier in front of them, so you can't make them anythingbut public.


I am sorry, but you do not understand me right.
What does the modifier of the method from an interface -
"virtual" or "virtual sealed"?
As far as I see by ILDASM - method called as virtual
(callvirt).
But VisualStudio do not add any modifier in front of this
method.
And I can not override this method in derived classes.
I should explicitly add word "virtual" in front of the
method.
Why?
Nov 22 '05 #4
<an*******@discussions.microsoft.com> wrote:
What does the modifier have the method MyClass::F() by
default?

Public. Interface implementation members are always

public, and you can't
put a visibility modifier in front of them, so you can't

make them anything
but public.


I am sorry, but you do not understand me right.
What does the modifier of the method from an interface -
"virtual" or "virtual sealed"?
As far as I see by ILDASM - method called as virtual
(callvirt).
But VisualStudio do not add any modifier in front of this
method.
And I can not override this method in derived classes.
I should explicitly add word "virtual" in front of the
method.
Why?


Why not? Why would you expect interface implementations to
automatically be virtual? They may be *called* virtually via the
interface, but I see no reason to make them virtual (in terms of being
able to be overridden) by default.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #5

<an*******@discussions.microsoft.com> wrote in message
news:e5****************************@phx.gbl...
I am sorry, but you do not understand me right.
What does the modifier of the method from an interface -
"virtual" or "virtual sealed"?
None of the above.
As far as I see by ILDASM - method called as virtual
(callvirt).
That's for security purposes. It ensures that the method is not being called
on an invalid object pointer. .virtcall is used most everywhere except for
static methods.
But VisualStudio do not add any modifier in front of this
method.
Well, it will add 'public' unless you're using explicit implementation.
And I can not override this method in derived classes.
I should explicitly add word "virtual" in front of the
method.
Why?


Because that's the way .NET works with interfaces. AFAIK as long as there
are matching accessible members of the class that match the ones on the
interface then everyone's happy.

--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

Nov 22 '05 #6
>Why not? Why would you expect interface implementations
to automatically be virtual?
Because interfaces in Java and in C++(pure abstract
classes) works this way.
Nov 22 '05 #7
But how does CLR discern where is plain function from
interface and where is virtual function from interface?
See code below.

interface I
{
void F();
}

class Plain : I
{
public void F(){}
}

class Virt : I
{
public virtual void F(){}
}

As far as I see - ILDASM generate the same .callvirt.
Nov 22 '05 #8
Vladimir Bezugliy <vb**@inbox.ru> wrote:
Why not? Why would you expect interface implementations

to automatically be virtual?

Because interfaces in Java and in C++(pure abstract
classes) works this way.


It's not *interfaces* that work that way in Java - it's *all* methods
which are virtual by default.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #9
Vladimir,
As far as I see - ILDASM generate the same .callvirt.


C# code almost always use the callvirt instruction regardless whether
the callee actually is virtual or not. The reason is that callvirt
checks if the this reference is null and throws a
NullReferenceException if it is, which is the specified behavior for
C#.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 22 '05 #10

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

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
9
by: Vladimir Bezugliy | last post by:
I have next interface: interface I { void F(); } And next class implements this interface: class MyClass : I { void F(){};
9
by: jlopes | last post by:
There seems to bet no diff between a vitual method and an inheirited method. class A_Base { public: virtual void filter(){ /* some code */ } }; class D_of_A_Base : public A_Base {
10
by: Mark | last post by:
I have an abstract class, and a set of classes that inherit from my abstract class. The fact that it is abstract is likely irrelevant. I have a static factory method in my abstract class that...
15
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } ...
10
by: Markus Svilans | last post by:
Hi, I have a weird problem in a virtual method. The original method code raises an access violation when it is run. The solution to the problem is to declare a dummy integer inside the virtual...
2
by: Kevin Frey | last post by:
In a derived class I am trying to redefine the implementation of a interface method defined in a base class - the base class interface method was not declared virtual. I have yet to actually...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
8
by: Belebele | last post by:
Suppose that I want to write a (concrete) interface class (without virtual functions) to classes that contain a method template // -------------------------------------- class Interface {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.