Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Barn Door

.net interfaces

This topic is 5366 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!