Skip to main content
GameDev.net gamedev.net
🔒 Locked

.net interfaces

Started by Barn Door Mar 4, 2004 at 4:09 PM 7 replies 550+ views
Original Post
Barn Door
Barn Door
Hi, If a class implements an interface but doesn''t declare the methods doing the implementing as virtual then a derived class cannot, it seems, override the base classe''s implementation. However, do the methods in the base class doing the implementation still get looked up at runtime in the same way as a virtual function would be. In otherwords, are they late bound? Cheers, BD.
Cedric
Cedric
The MSDN library reference on C# seemed pretty clear to me on the issue of inheritance and overriding.

And, though you haven't mentionned it, are you using C# or VB? I'll assume C#.
quote:
Original post by Barn Door
If a class implements an interface but doesn't declare the methods doing the implementing as virtual then a derived class cannot, it seems, override the base classe's implementation.
IIRC, you need to use the keyword "override", and not "virtual" to implement a virtual function (virtual is used in base classes). If you don't mention "override", then you are, IIRC again, hiding the base function. In the case of an interface, you won't be allowed to do that, since you have to implement all of the interface's function.
quote:
However, do the methods in the base class doing the implementation still get looked up at runtime in the same way as a virtual function would be. In otherwords, are they late bound?
If you are hiding by not using "override", then my guess would be that they are not late bound. It would be very easy to test it, though.

EDIT: On second thought, it's not a "guess"; it's common sense. If it wasn't the case, "hiding" would be the same as "overriding".

Cédric

[edited by - Cedric on March 4, 2004 8:29:00 PM]
Barn Door
Barn Door
I'm using vb.net.

I'd have though they'd have to be late bound.

Lets say that you have a class in the .net framework which programmes an IEnumerator. (For example, an asp.net Repeater upon 'Databind')

If I create my own container class and implement IEnumerator with it and plug an instance of this into the Repeater, then the only way that the Repeater could know what implementation of IEnumerator to use would be to look it up at runtime - you don't recompile the .net framework!

If all interface methods are late bound then I think its curious that you can effectively have a virtual function which you can't override.

How is it easy to test whether a function is late bound or linked at compile time?

What do you think?

[edited by - barn door on March 4, 2004 8:51:23 PM]

[edited by - barn door on March 4, 2004 8:51:52 PM]
Cedric
Cedric
You misunderstood.

AFAIK, interfaces only have virtual functions. I would venture to say that the virtual keyword is probably not necessary, as it is implicit.

quote:
If all interface methods are late bound
They are.
quote:
then I think its curious that you can effectively have a virtual function which you can''t override.
?How? Can you provide an example of what you mean?

quote:
Original post by Barn Door
How is it easy to test whether a function is late bound or linked at compile time?
Create a dummy class, and another class that inherits from it. Then call the function with a pointer-to-base.

Cédric
Barn Door
Barn Door
Hi,

Look here...

http://www.superdotnet.com/csharp_onlinespec/2010.aspx

In particular...

'Without explicitly re-implementing an interface, a derived class cannot in any way alter the interface mappings it inherits from its base classes. '

'Since explicit interface member implementations cannot be declared virtual, it is not possible to override an explicit interface member implementation.'

quote:
quote:
If all interface methods are late bound

They are.


Why did you say that its common sense that they're not late bound?

BD.

[edited by - barn door on March 4, 2004 9:48:00 PM]
Cedric
Cedric
Whoa, looking back at the thread, I realize that not only have I not read your original question properly, I have also spread quite a bit of information that is downright wrong .

My apologies.

I underestimated the problem. Now, you seem to know more about that stuff than I do, but I may try an answer...
quote:
http://www.superdotnet.com/csharp_onlinespec/2010.aspx

In particular...

'Without explicitly re-implementing an interface, a derived class cannot in any way alter the interface mappings it inherits from its base classes. '

'Since explicit interface member implementations cannot be declared virtual, it is not possible to override an explicit interface member implementation.'
EDIT: Errr... My answer is wrong again! Go me. I'm going to sleep now. I need it. Badly.

[edited by - Cedric on March 4, 2004 11:30:10 PM]
Cedric
Cedric
Ok, I think that I have some answers. Perhaps you''ve found them too, so if you don''t agree with what I say, please tell me.
quote:
Original post by Barn Door
If a class implements an interface but doesn''t declare the methods doing the implementing as virtual then a derived class cannot, it seems, override the base classe''s implementation.
Unless you reimplement the interface.
quote:
However, do the methods in the base class doing the implementation still get looked up at runtime in the same way as a virtual function would be. In otherwords, are they late bound?
If you don''t reimplement, then you are "hiding".

hiding = early bound
overriding = late bound

HTH,

Cédric
Barn Door
Barn Door
I don''t think vb.net lets you reimplement at all.

Anyhow, there''s a deeper issue here that I''m clueless about. That is, what is the difference between a virtual and non-virtual function call in .net?

Given that objects are potentially loaded from different assemblies, isn''t everything late bound to some degree?

I don''t understand how .net does linking.
Cedric
Cedric
I was hoping that someone more knowledgeable would answer, but let me provide my 2 cents....
quote:
Original post by Barn Door
Anyhow, there''s a deeper issue here that I''m clueless about. That is, what is the difference between a virtual and non-virtual function call in .net?
I think it''s simply that virtual=late-bound and non-virtual=early-bound
quote:
Given that objects are potentially loaded from different assemblies, isn''t everything late bound to some degree?
Hmmm... Possibly. I don''t know .net well enough. Is it possible to load an assembly at run-time? If not, then the early-bound methods could be bound during the "compilation to native code" step at the start-up of the app.

Cédric

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.