Christian Nagel's OneNotes

.NET Training, Consulting, Coaching - C#, Web Services, Enterprise Services, ASP.NET, Whidbey, Longhorn and More!

Sponsors

Affiliations

Books I've written

INETA UG Leaders

My Blogroll

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.

Christian

Comments

Kathleen Dollard said:

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#.
# May 9, 2004 2:01 PM

Kathleen Dollard said:

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.
# May 9, 2004 2:04 PM

TrackBack said:

# May 12, 2004 5:10 PM

TrackBack said:

Accidental or intentional?
# May 13, 2004 9:11 AM

siva said:

i can't under this example pls clearly

# December 3, 2008 12:43 AM

TamusJRoyce said:

abstract class in vb .net is called MustInherit, and therefore

"Such a scenario seems not possible with VB"

is not only possible, but identical in functionality with C#.

The main point in this article has overlooked this:

public interface IA

  Sub A()

End IA

public mustinherit class XA

  Public Sub A()

  End Sub

End Class

public class X

Inherits XA

Implements IA

End Class

# August 13, 2009 11:12 AM

ramya said:

it is better to give an example program which helps to learn more

# September 14, 2010 1:51 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)