One day to go

Interface Inheritance

Memi Lavi writes about inherits vs. implements in C#, and that the difference gets much more obvious with VB. He also talks about multiple inheritance with interfaces is not possible.

Mostly I'm writing my programs with C# (sometimes C++ and VB). Now I've done some VB code to see the difference.

Multiple inheritance is possible with interfaces, and VB also uses the Inherits keyword here:

Public Interface IA
    Sub A()
End Interface

Public Interface IB
    Sub B()
End Interface

Public Interface IC
    Inherits IA, IB
    Sub C()
End Interface

Multiple inheritance with interfaces.

With C# it is possible that a class derives from an interface but gets the interface implementation from an abstract base class. The C# code is here:

public interface IA
   void A();

public abstract class XA
   public void A() { }

public class X : XA, IA  // get the implementation of IA from XA

Such a scenario seems not possible with VB. With the Implements keyword of VB, always an implementation is needed.

Of course the code can be changed, so that the abstract class implements the interface.



Feed You can follow this conversation by subscribing to the comment feed for this post.

Kathleen Dollard

public class X : XA, IA // get the implementation of IA from XA { } VB.NET is more explicit in how it handles interfaces. In my opinion this is a very good thing. So, the above scenario, would not work by itself. It would look like Public Class X Inherits XA Implements IA Public Sub WhateverName() Implements IA.A MyBase.A() End Sub End Class Yes, this is a few more lines of code, but it means that you know how and where X is getting its implementation. VB.NET thrives on being explicit. You have to work harder to find out what is implicitly happening in C#.

Kathleen Dollard

For clarification... The benefit of explicitness here is both discoverability _and_ control. VB.NET is more flexible in the above scenario if XA.A does not implement IA.A, but implements some other behavior, leaving another method to implement the IA.A behavior. VB.NET also allows the same syntax when you wish to have different names/visibility for the implementation directly from the class vs. from the interface. C# private implementations use a different syntax.

Geek Noise

AUTHOR: Geek Noise
DATE: 05/12/2004 11:10:00 PM

Panopticon Central

Accidental or intentional?

christian louboutin

I have just finished the article.It is the first time for me to read the whole post carefully,I always just look through the article,and forget quickly.But I had save your articl,it is more than useful to me.

UGG Shoes

oh so funny!

coach outlet online

As long as there is heart there is hope, there is happiness there

coach outlet online

I agree you the up.As it give me the same some feeling!

The comments to this entry are closed.